首页 > 解决方案 > 请求访问令牌时 Owin 未通过身份验证

问题描述

我正在使用隐式授权类型,当我请求“id_token token”作为响应类型时,我的 HttpContext.Current.User 在登录后为空,导致我相信 owin 内部出现问题。如果我只有“id_token”作为响应类型就可以了。我需要告诉 owin 在某个地方获取访问令牌吗?

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

标签: owinidentityserver4

解决方案


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

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

我在这里有完整的工作样本


推荐阅读