【问题标题】:Getting id_token from IdentityServer4?从 IdentityServer4 获取 id_token?
【发布时间】:2018-09-11 14:30:34
【问题描述】:

我对 IdentityServer4 服务器进行了以下流程设置:

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

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

            options.ClientId = "mvc";
            options.ClientSecret = "secret";
            options.ResponseType = "code id_token";

            options.SaveTokens = true;
            options.GetClaimsFromUserInfoEndpoint = true;

            options.Scope.Add("api1");
            options.Scope.Add("offline_access");
            options.ClaimActions.MapJsonKey("website", "website");

        });

效果很好,用户身份验证正确。

我想访问响应中的 id_token,以便查看用户显示名称。

我该怎么做?我环顾了 HttpContext.User,但没有找到太多。

【问题讨论】:

    标签: .net asp.net-mvc oauth-2.0 identityserver4


    【解决方案1】:

    如果用户的身份验证正确,那么在 MVC 控制器中,您将能够将用户强制转换为 ClaimsPrincipal 并访问用户的声明:

    ClaimsPrincipal user = User as ClaimsPrincipal;
    Claim nameClaim = user.FindFirst("name");
    string name = nameClaim.Value
    

    【讨论】:

      猜你喜欢
      • 2017-05-19
      • 2017-05-19
      • 2017-07-25
      • 1970-01-01
      • 2020-03-11
      • 2021-01-27
      • 2017-04-04
      • 2023-03-27
      • 2018-08-25
      相关资源
      最近更新 更多