繁体   English   中英

如何获取访问令牌和用户信息:使用 AspNet Identity 的 IdentityServer4

[英]How to Get Access Token and User Info: IdentityServer4 With AspNet Identity

今天是个好日子,

因此,我成功地将使用 AspNet Identity 进行身份验证的 IdentityServer 4 集成到我的项目中。 它在身份验证后重定向到客户端,这样就可以了。 现在的问题是我想在客户端获取用户详细信息,例如他们的用户名 email、名字和姓氏。 我想用令牌调用 userinfo 端点,但我无法获得令牌。 我收到以下错误:

  1. 客户端未授权客户端凭据流,检查 AllowedGrantTypes 设置 - 当我在客户端配置中使用GrantType.Authorization Code
  2. 客户端无法在客户端凭据流中请求 openid 范围 - 当我使用GrantType.ClientCredentials

客户端已在 startup.cs 中设置为;

services.AddAuthentication(options =>
            {
                options.DefaultScheme = "cookie";
                options.DefaultChallengeScheme = "oidc";
            })
            .AddCookie("cookie")
            .AddOpenIdConnect("oidc", options =>
            {
                options.Authority = ConfigurationManager.AppSettings["AuthorityUrl"];
                options.ClientId = ConfigurationManager.AppSettings["ClientId"];
                options.ClientSecret = ConfigurationManager.AppSettings["ClientSecret"];
                options.RequireHttpsMetadata = false;
                options.GetClaimsFromUserInfoEndpoint = true;

                options.ResponseType = "code";
                options.UsePkce = true;
                options.ResponseMode = "query";

                options.Scope.Add("openid");
                options.Scope.Add("profile");
                options.SaveTokens = true;

            });

我目前的客户端配置如下;

new Client
                    {
                         ClientName = "Admin Client",
                         ClientId = "admin.client",
                         AllowedGrantTypes = GrantTypes.Code,
                         ClientSecrets = { new Secret("secretkey".Sha256()) },
                         AllowedScopes =new List<string> {
                               IdentityServerConstants.StandardScopes.OpenId,
                               IdentityServerConstants.StandardScopes.Profile
                            },
                         RedirectUris = {"https://localhost:44324/signin-oidc"},
                         FrontChannelLogoutUri = "https://localhost:44324/signout-oidc",
                         PostLogoutRedirectUris = {"https://localhost:44324/signout-callback-oidc"},

                         AllowOfflineAccess = true,
                         RequirePkce = true,
                         RequireConsent = false,
                         AllowPlainTextPkce = false

                    }

为了获得令牌,我这样做(当我在客户端配置中使用GrantType.ClientCredentials时;

using var client = new HttpClient();
            var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
            {
                Address = _discoveryDocument.TokenEndpoint,
                ClientId = ConfigurationManager.AppSettings["ClientId"],
                ClientSecret = ConfigurationManager.AppSettings["ClientSecret"],
                Scope = "openid"

            });

我这样称呼用户:

var response = await client.GetUserInfoAsync(new UserInfoRequest
            {
                Address = _discoveryDocument.UserInfoEndpoint,
                Token = token
            });

但它永远不会到达用户信息。 请注意,我一直在将客户端配置中的 GrantTypes 更改为 Hybrid、AuhorizationCode,但它仍然不起作用。 当我使用 AuthorizationCode grantType 和 Use RequestAuthorizationCodeTokenAsync时,它在参数中请求代码,但我不知道如何获取授权代码(抱歉,我是新手)。

那么请问我应该如何配置我的客户端,以便能够请求我可以在 userinfo 端点中使用的令牌,或者有什么方法可以获取用户信息?

请帮忙。 谢谢。

要获取您使用 Claims 的用户详细信息

基本上你这样称呼它

@User.Claims.FirstOrDefault(c => c.Type == "name")?.Value (在 cshtml 页面上)

或者

User.Claims.Where(c => c.Type == "mail").First().Value (on.cs 类)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM