Transforming Open Id Connect claims in ASP.Net Core

前端 未结 4 841
时光说笑
时光说笑 2020-12-30 05:58

I\'m writing an ASP.Net Core Web Application and using UseOpenIdConnectAuthentication to connect it to IdentityServer3. Emulating their ASP.Net MVC 5 sample I\'

4条回答
  •  遥遥无期
    2020-12-30 06:27

    I personally prefer to do the claims transformation in the middleware where the actual authentication happens.

    You can use the OnTicketReceived event on the OIDC middleware for that.

    var oidcOptions = new OpenIdConnectOptions
    {
       AuthenticationScheme = "oidc",
       SignInScheme = "cookies",
    
       Authority = Clients.Constants.BaseAddress,
    
       ClientId = "mvc.hybrid",
       ClientSecret = "secret",
       ResponseType = "code id_token",
       SaveTokens = true,
    
       TokenValidationParameters = new TokenValidationParameters
       {
          NameClaimType = JwtClaimTypes.Name,
          RoleClaimType = JwtClaimTypes.Role,
       },
    
       Events = new OpenIdConnectEvents
       {
           OnTicketReceived = e =>
           {
               ClaimsPrincipal p = TransformClaims(e.Ticket.Principal);
               e.Ticket = new AuthenticationTicket(
                p,
                e.Ticket.Properties,
                e.Ticket.AuthenticationScheme);
    
            return Task.CompletedTask;
        }
      }
    };
    

提交回复
热议问题