Transforming Open Id Connect claims in ASP.Net Core

前端 未结 4 821
时光说笑
时光说笑 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:38

    Thank you Adem for your reply... it solved the vast majority of the problem... the only issue being that identity.Claim is a read only property. I found creating a new Principal did work though:

    Events = new CookieAuthenticationEvents()
    {
        OnSigningIn = (context) =>
        {
            ClaimsIdentity identity = (ClaimsIdentity)context.Principal.Identity;
    
            var givenName = identity.FindFirst(Constants.ClaimTypes.GivenName);
            var familyName = identity.FindFirst(Constants.ClaimTypes.FamilyName);
            var sub = identity.FindFirst(Constants.ClaimTypes.Subject);
    
            var claimsToKeep = new List {givenName, familyName, sub};
    
            var newIdentity = new ClaimsIdentity(claimsToKeep, identity.AuthenticationType);
    
            context.Principal = new ClaimsPrincipal(newIdentity);
    
            return Task.FromResult(0);
        }
    }
    

    Whether this is the correct approach I'm not sure, but it appears to work.

提交回复
热议问题