【问题标题】:Notifications in ASP.NET Core client for IdentityServer v4IdentityServer v4 的 ASP.NET Core 客户端中的通知
【发布时间】:2017-02-28 00:55:34
【问题描述】:

在 IdentityServer 3 中,我使用了通知上的 SecurityTokenValidated 事件来建立我自己的带有名称和声明的身份。例如,我存储access_token 以供以后使用资源所有者工作流访问 n API:

app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
AuthenticationType = "oidc",

// ...

Notifications = new OpenIdConnectAuthenticationNotifications
{
    SecurityTokenValidated = async n =>
    {
        var nid = new ClaimsIdentity(
          n.AuthenticationTicket.Identity.AuthenticationType,
          "name",
          ClaimTypes.Role);
        nid.AddClaim(new Claim("id_token", n.ProtocolMessage.IdToken));
        nid.AddClaim(new Claim("access_token", n.ProtocolMessage.AccessToken));
        nid.AddClaim(new Claim("expires_at", DateTimeOffset.Now.AddSeconds(int.Parse(n.ProtocolMessage.ExpiresIn)).ToString()));
    }
}
}

在 IdentityServer 4 for ASP.NET Core 中没有 Notifications 属性。 我可以看到自动生成了很多声明,但我没有得到access_token,也没有自动设置身份的用户名

我当前在 ASP.NET Core 中的客户端配置如下所示

   app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
        {
            AuthenticationScheme = "oidc",
            SignInScheme = "Cookies",
            Authority = identityServerUri,
            RequireHttpsMetadata = false,
            ClientId = clientId,
            ResponseType = "id_token token",
            Scope =
            {
                "openid profile email warehouseapi"
            },
            GetClaimsFromUserInfoEndpoint = true,
            SaveTokens = true,
            AutomaticAuthenticate = true,
            AutomaticChallenge = true,
        });

IdentityServer 4 中的预期方式是什么?

【问题讨论】:

    标签: asp.net-core asp.net-core-mvc identityserver3 asp.net-core-1.0 identityserver4


    【解决方案1】:

    您可以使用TickedReceived 事件来转换声明:

            var oidcOptions = new OpenIdConnectOptions
            {
                ...
                Events = new OpenIdConnectEvents()
                {
                    // get access token
                    OnTicketReceived = ctx =>
                    {
                        // transform claims
                        var access_token = ctx.Ticket.Properties.GetTokenValue("access_token");
                        return Task.FromResult(0);
                    }
                }
            };
    

    您也不需要将令牌保存为声明,因为当您将 SaveTokens 设置为 true 时,令牌会自动保存在身份验证属性中。要获取令牌,您可以使用HttpContext.Authentication.GetTokenAsync("<token name>")

    【讨论】:

    • thx 事件有效,但在我的 asp 核心项目中没有 GetTokenAsync 方法和 HttpContext.Authentication
    • 尝试使用添加Microsoft.AspNetCore.Authentication
    • 你想去哪里获取token?如果在TickedReceived事件中,无法通过这种方式获取token,此时可以使用var token = ctx.Ticket.Properties.GetTokenValue("access_token");
    【解决方案2】:

    这实际上与 IdentityServer4 无关。 OWIN 和 AspNetCore 变体中的身份验证中间件之间的差异更大。

    这些通知现在更正确地命名为Events

    您可以使用以下方法执行类似操作:

    app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
    {
        AuthenticationScheme = "oidc",
        SignInScheme = "Cookies",
    
        Authority = "https://demo.identityserver.io",
        PostLogoutRedirectUri = "http://localhost:3308/",
        ClientId = "hybrid",
        ClientSecret = "secret",
        ResponseType = "code id_token",
        GetClaimsFromUserInfoEndpoint = true,
        SaveTokens = true,
    
        Events = new OpenIdConnectEvents
        {
            OnTokenValidated = async n =>
            {
    
            }
        }
    });
    

    您可以找到所有精彩活动here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-02
      • 1970-01-01
      • 2016-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多