Base solution for your next web application
Open Closed

Custom Field - AbpSession #676


User avatar
0
maharatha created

To start with I am in love with Abp and AbpZero. Awesome Job @hikalkan.

I have read the previous article of extending the session ields :

#584@1182f777-3cd5-40b6-82d9-19e91497527f

I found the easiest way to do is to add claims directly but I don't think that's the smartest way to do it.

Could you provide an example with implementation as I am sure this is going to help a lot of people ? I am actually planning to store the organization ID for a user.


9 Answer(s)
  • User Avatar
    0
    maharatha created

    This is how I am adding claims :

    var identity = await UserManager.CreateIdentityAsync(GetCurrentUser(), DefaultAuthenticationTypes.ApplicationCookie);

    identity.AddClaim(new Claim("OrganizationId", "2"));

    This is how I am trying to get the claim :

    public class MyAppSession : IdentityFrameworkClaimsAbpSession {

        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        private readonly UserManager _userManager;
    
        public MyAppSession(UserManager userManager, IMultiTenancyConfig multiTenancy) : base(multiTenancy)
        {
            
            _userManager = userManager;
        }
    
       
        public static long? OrganizationId
        {
            get
            {
    
                
                var claimsPrincipal = Thread.CurrentPrincipal as ClaimsPrincipal;
                if (claimsPrincipal == null)
                {
                    return null;
                }
    
                var claimsIdentity = claimsPrincipal.Identity as ClaimsIdentity;
                if (claimsIdentity == null)
                {
                    return null;
                }
                
                var OrgIdClaim = claimsIdentity.Claims.FirstOrDefault(c => c.Type == "OrganizationId");
                if (OrgIdClaim == null || string.IsNullOrEmpty(OrgIdClaim.Value))
                {
                    return null;
                }
    
                long orgId;
                if (!long.TryParse(OrgIdClaim.Value, out orgId))
                {
                    return null;
                }
    
                return orgId;
            }
          
        }
    
    
    
        
    }
    

    I am unable to get the claim :

    Am I doing anything wrong ?

  • User Avatar
    0
    hikalkan created
    Support Team

    I prepared a Gist demonstrates it: <a class="postlink" href="https://gist.github.com/hikalkan/67469e05475c2d18cb88">https://gist.github.com/hikalkan/67469e05475c2d18cb88</a> It's same also for AspNet Zero.

  • User Avatar
    0
    maharatha created

    Awesome !! Worked for me.

    I am currently starting a new project so such quick response is very much appreciated.

    Thank You Again .

  • User Avatar
    0
    JeffMH created

    Just FYI, this no longer works because the AccountController login method no longer call SignInAsync in the account controller.

    <a class="postlink" href="https://gist.github.com/hikalkan/67469e05475c2d18cb88">https://gist.github.com/hikalkan/67469e05475c2d18cb88</a>

    I updated this with a question. Just making sure no one missed the question.

    Thanks!

  • User Avatar
    0
    JeffMH created

    Any quick hints for this? I am about to have to really tear apart some of the code, and it's going to take quite a bit of time. I was hopeful someone had an "oh that's easy" answer.

  • User Avatar
    0
    bilalhaidar created

    Oh, this I want too! Any help would be appreciated.

  • User Avatar
    0
    JeffMH created

    Ok, I figured it out. In the UserManager class within the Core project, override the CreateIdentityAsync method. You can add your claim in here. SignInManager calls this function. I will update the GitHub link.

    public override async Task<ClaimsIdentity> CreateIdentityAsync(User user, string authenticationType)
    {
        var identity = await base.CreateIdentityAsync(user, authenticationType);
    
        //- Custom claim here
        identity.AddClaim(new Claim("MyCustomClaim", "ClaimValue"));
    
        return identity;
    }
    

    And by the way, sorry to keep harping on this @drcgreece, this is one of those places where the pros / cons of not changing their code is a tough one. You can either modify the class they gave you, or you have to Implement your own UserManager that inherits from theirs, and within the IOC config, you can change how the IUserManager interface is created. To me, it's just a little more than I want to do. Your mileage may very.

    Sorry, I don't mean to sound so repetitive, I just thought this was a good example of how the lines get blurred as you go.

  • User Avatar
    0
    bilalhaidar created

    Jeff, that was so helpful!

    I came to conclusion better to use Git and follow those steps you mentioned.

    Add to this, if I group my code changes in for example 1 folder that would be great as it makes merging easier. Also, in some case, I need for sure to update existing files outside that "magical" folder, like in this case adding a field to AbpSession, but a least and hoping this would be special case.

    Makes sense?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @JeffMH

    Thanks a lot :). I have missed this topic somehow. Thanks again for your help.