Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Joel Mandell 6 posts 75 karma points c-trib
    Mar 15, 2022 @ 11:58
    Joel Mandell
    0

    Adding Claims to MemberIdentityUser.Claims - gets cleared after SignInAsync.

    Hi! I am authenticating credentials against an external API and if successful I'm creating a MemberIdentityUser()-object that I sign in with the _memberSignInManagaer. And adding roles work correctly.

    But as soon as I try to add Claims on the MemberIdentifyUser-object, they are cleared. When iterating the claims on an razor page for example (for debugging purpose) they are not there. Why is that so?

     var member = new MemberIdentityUser();
     member.Email = Entity.Email;
     member.Name = Entity.Name;
     member.UserName = Entity.UserName;
    
     //Add member role as default.
     member.AddRole(memberRole);
    
     if (Entity.HasGradeAccess)
     {
         member.AddRole(chairmanRole);
     }
    
     //Sign in with lime integrated user.
     await _memberSignInManager.SignInAsync(member, true);
    
  • Joel Mandell 6 posts 75 karma points c-trib
    Mar 20, 2022 @ 21:41
    Joel Mandell
    0

    Okay. It seems that you can actually add Claims in the above code... Then you have to implement an class, for example "AdditionalUserClaimsPrincipalFactory", that inherits from UserClaimsPrincipalFactory

    So you could do then re-add the claims added previously in this code below and they will persist until signing out.

    Code is from this link in similar post on umbraco forum: https://our.umbraco.com/forum/using-umbraco-and-getting-started/108270-how-to-add-custom-claim-to-the-loggedin-member-in-umbraco-9

    public class AdditionalUserClaimsPrincipalFactory : UserClaimsPrincipalFactory<MemberIdentityUser, UmbracoIdentityRole>
    {
        public AdditionalUserClaimsPrincipalFactory(
            UserManager<MemberIdentityUser> memberManager,
            RoleManager<UmbracoIdentityRole> roleManager,
            IOptions<IdentityOptions> optionsAccessor)
            : base(memberManager, roleManager, optionsAccessor)
        {
        }
    
        public async override Task<ClaimsPrincipal> CreateAsync(MemberIdentityUser user)
        {
            var principal = await base.CreateAsync(user);
    
            var identity = (ClaimsIdentity)principal.Identity;
            foreach (var claim in user.Claims)
            {
                identity.AddClaim(new(claim.ClaimType, claim.ClaimValue));
            }
    
            return principal;
        }
    }
    
  • andrew shearer 506 posts 653 karma points
    Nov 04, 2022 @ 01:27
    andrew shearer
    0

    Hi Joel - was this still the solution you ended up going with? I don't want to set extra Claims as such but the groups the member belongs to, but can't find documentation on how to do that.

    thanks

    Andrew

  • Joel Mandell 6 posts 75 karma points c-trib
    Jul 23, 2023 @ 00:30
    Joel Mandell
    0

    @andrew shearer This video might give you some hints on how to do it: https://youtu.be/aouFfym_7Zs

Please Sign in or register to post replies

Write your reply to:

Draft