【问题标题】:Get accessToken in Identity Server ASP.NET Core在 Identity Server ASP.NET Core 中获取 accessToken
【发布时间】:2019-10-03 13:40:30
【问题描述】:

我将 IdentityServer 与 ASP.NET Core MVC 应用程序一起使用。

在 IdentityServer 中,我的客户端定义如下:

new Client
    {
        ClientId = "admin.client",
        ClientName = "Admin Application",
        AllowedGrantTypes = GrantTypes.Implicit,
        ClientSecrets =
                        {
                            new Secret("secret".Sha256())
                        },
        RedirectUris = { "http://localhost:45876/signin-oidc" },
        PostLogoutRedirectUris = { "http://localhost:45876/signout-callback-oidc" },
        AllowedCorsOrigins = new[] { "http://localhost:45876/" },
        AllowedScopes =
                        {
                            IdentityServerConstants.StandardScopes.OpenId,
                            IdentityServerConstants.StandardScopes.Profile
                        },
        AllowOfflineAccess = true
}

客户注册如下:

services.AddAuthentication(
            options =>
                {
                    options.DefaultScheme = "Cookies";
                    options.DefaultChallengeScheme = "oidc";
                })
            .AddCookie("Cookies")
            .AddOpenIdConnect(
                "oidc",
                options =>
                    {
                        options.SignInScheme = "Cookies";

                        options.Authority = "http://localhost:5001/";
                        options.RequireHttpsMetadata = false;


                        options.ClientId = "admin.client";
                        options.ClientSecret = "secret";
                        options.ResponseType = "code id_token";
                        options.SaveTokens = true;
                        options.GetClaimsFromUserInfoEndpoint = true;

                        options.Scope.Add("openid");
                        options.Scope.Add("profile");
                    });

我的问题是当我想获取为空的 accessToken 时。

我正在尝试使用此代码获取它:

 var token = await _httpContextAccessor.HttpContext.GetTokenAsync("access_token");
 string accessToken = User.FindFirst("access_token")?.Value;

它们都返回 null。

我的问题在哪里?

【问题讨论】:

  • 您的登录没有问题?用户已通过身份验证?
  • 是的,使用已通过身份验证,我可以获得 User.Claims。

标签: c# asp.net-core identityserver4


【解决方案1】:

第一个问题是您将客户端应用程序配置为使用 Hybrid flow 与 IdentityServer 进行交互:

 options.ResponseType = "code id_token";

但客户端配置只允许使用Implicit flow

 AllowedGrantTypes = GrantTypes.Implicit,

这将在身份验证请求期间引发invalid grant type 错误。

第二个问题是你没有配置ApiResource

public static IEnumerable<ApiResource> GetApis()
{
    return new List<ApiResource>
    {
        new ApiResource("api1", "My API")
    };
}

客户端也没有请求访问api资源的权限,虽然你可以让你的客户端应用程序通过身份服务器对用户进行身份验证,但它会返回ID token,而不是Access token

  • ID token 至少包含用户的标识符(称为 sub aka 主题声明)以及有关用户如何以及何时进行身份验证的信息

  • Access token 允许访问 API 资源,包含有关客户端和用户的信息(如果存在)。

由于您使用的是 Web 应用程序(mvc 作为客户端应用程序)。使用 hybrid flow 的更好选择,它同时启用 API 访问和用户身份验证。 Here 是代码示例。

【讨论】:

    猜你喜欢
    • 2018-06-03
    • 2021-04-19
    • 2016-12-09
    • 2021-01-22
    • 2023-03-16
    • 2018-01-08
    • 2020-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多