在web api客户端访问令牌客户端IdSrv3身份验证
问题描述:
我正在使用IdSrv3进行身份验证。我需要在我的web api owin客户端中获取access_token,以在另一个web api客户端传递承载认证。 我Startup.cs代码:在web api客户端访问令牌客户端IdSrv3身份验证
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?声明变量是空的。
答
您可以将“PreserveAccessToken”属性设置为true(默认为false)。例如:
app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
{
Authority = "https://localhost:44331",
ClientId = "apiOne",
ClientSecret = "secret",
RequiredScopes = new[] {"apiOne"},
ValidationMode = ValidationMode.ValidationEndpoint,
PreserveAccessToken = true
});
这会将访问令牌保留为声明。然后,您可以像上面那样进行检索。除此之外,声明是“标记”,而不是“access_token”。
+0
它再次为空:( –
+0
@AstemirAlmov - 那么,根本没有声明?可以发布显示其他作品配置的代码吗?例如,客户端调用该Web API和该客户端的IdentityServer配置? –
在那里有问题吗? :) –
@JohnKorsnes修正:) –
酷:)请为下一个开发人员提供一个答案,如果它可以使其他人受益:) –