【问题标题】:Access token in web api client IdSrv3 authenticationWeb api 客户端 IdSrv3 身份验证中的访问令牌
【发布时间】:2016-12-08 19:28:21
【问题描述】:

我正在使用 IdSrv3 进行身份验证。我需要在我的 web api owin 客户端中获取 access_token 以在另一个 web api 客户端中通过承载身份验证。 我的 Startup.cs 代码:

public class Startup
{
        public void Configuration(IAppBuilder app)
        {

            ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

            JwtSecurityTokenHandler.InboundClaimTypeMap.Clear();

            var identityServerPath = ConfigurationManager.AppSettings["IdentityServerPath"];

            app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
            {
                Authority = $"{identityServerPath}/core",

                RequiredScopes = new[] { "openid"},
                AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Active,

                // client credentials for the introspection endpoint
                ClientId = "someid"
            });
        }
}

我正在尝试通过这种方式获取访问令牌:

var claims = (User as ClaimsPrincipal).Claims;
var AccessToken = claims.First(x => x.Type == "access_token").Value;

如何获取access_token?声明变量为空。

【问题讨论】:

  • 那里有问题吗? :)
  • @JohnKorsnes 已修复 :)
  • 酷 :) 请为下一个开发者提供答案,如果它可以造福他人:)

标签: c# authentication asp.net-web-api openid-connect identityserver3


【解决方案1】:

您可以将“PreserveAccessToken”属性设置为 true(默认为 false)。例如:

app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
{
    Authority = "https://localhost:44331",
    ClientId = "apiOne",    
    ClientSecret = "secret",
    RequiredScopes = new[] {"apiOne"},
    ValidationMode = ValidationMode.ValidationEndpoint,
    PreserveAccessToken = true
});

这会将访问令牌保留为声明。然后,您可以像上面那样检索它。除了声明是“token”,而不是“access_token”。

【讨论】:

  • 又是空的:(
  • @AstemirAlmov - 那么,根本没有索赔吗?也许您可以发布显示其他部分配置的代码?例如,调用此 Web API 的客户端和该客户端的 IdentityServer 配置?
猜你喜欢
  • 1970-01-01
  • 2016-01-22
  • 2013-08-19
  • 1970-01-01
  • 1970-01-01
  • 2014-04-29
  • 1970-01-01
  • 1970-01-01
  • 2017-02-18
相关资源
最近更新 更多