c# - Web api 客户端 IdSrv3 身份验证中的访问 token

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

我正在使用 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"
            });
        }
}

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

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
});

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

关于c# - Web api 客户端 IdSrv3 身份验证中的访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38737430/

相关文章:

c# - Winforms中的Tag属性有什么用?

ruby - Meteor 使用 bcrypt 验证来自不同服务器的电子邮件/密码

c# - 如何调试或跟踪 ASP.Net WebApi 2 (5.0) XML 序列化?

C# WebAPI 解析带有额外大括号的 Json 数据

javascript - 对象列表中的动态键名称

c# - 带有函数参数的重载函数的奇怪模糊调用

c# - 将字符串数组传递给 webservice 方法

c# - 动态改变jwplayer视频路径

android - Web View 身份验证

php - 在没有管理员连接的情况下使用 LDAP 在 Laravel 中进行身份验证/登录?