请求访问令牌时,Owin未通过身份验证

时间:2020-06-26 02:25:28

标签: owin identityserver4

我正在使用隐式授予类型,当我请求“ id_token令牌”作为响应类型时,登录后我的HttpContext.Current.User为null,这使我相信owin内部出现了问题。如果我只是将“ id_token”作为响应类型,则可以。我是否需要告诉owin以获得访问令牌?

作为参考,我使用.Net Framework作为客户端和Identityserver4。

1 个答案:

答案 0 :(得分:1)

要能够通过浏览器获取令牌,您需要在IdentityServer中的客户端配置上设置AllowAccessTokensViaBrowser = true

                new Client
                {
                    ...

                    AllowedGrantTypes = GrantTypes.Implicit,
                    AllowAccessTokensViaBrowser = true,

                    ...
                },

以及在MVC客户端的“启动”中,您可以向用户添加access_token作为声明:

app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
            {
                ...
                ResponseType = "id_token token",             

                Notifications = new OpenIdConnectAuthenticationNotifications
                {
                    SecurityTokenValidated = n =>
                    {
                        n.AuthenticationTicket.Identity.AddClaim(new Claim("access_token", n.ProtocolMessage.AccessToken));
                        
                        return Task.FromResult(0);
                    }
                }
            });

我有完整的工作示例here