Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Custom session property with .NET Core project #2333


User avatar
0
geckotechnical created

Hi,

I am adding custom properties to a session with the process outlined on this page: <a class="postlink" href="https://gist.github.com/hikalkan/67469e05475c2d18cb88">https://gist.github.com/hikalkan/67469e05475c2d18cb88</a>, this worked fine on an .NET MVC 5.x project, however implementing it in the same fashion on a .NET Core project causes a null reference error with HttpContext when adding a new cookie to the authentication manager.

Is there another way of achieving custom session properties with the new .NET Core project? I need to be able to add them in the overridden TryAuthenticateAsync method in a custom authentication layer.

Thanks


4 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    You can add custom claims before this line: <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero-core/blob/dev/aspnet-core/src/MyCompanyName.AbpZeroTemplate.Web.Mvc/Controllers/AccountController.cs#L155">https://github.com/aspnetzero/aspnet-ze ... er.cs#L155</a> like shown below:

    loginResult.Identity.AddClaim(...);
    

    Alternatively, you can override CreateIdentityAsync method in UserManager, like this:

    public override async Task<ClaimsIdentity> CreateIdentityAsync(User user, string authenticationType)
    {
        var identity = await base.CreateIdentityAsync(user, authenticationType);
        identity.AddClaim(...);
        return identity;
    }
    
  • User Avatar
    0
    geckotechnical created

    Hi,

    Thanks for the reply

    Apologies I should have mentioned it was the Angular 2 and .NET Core project, I don't have that controller in the server project.

    This is what I was using in another project that was .NET MVC 5.x, if possible I'd like for it to be achieved in a similar fashion:

    public string UserToken
            {
                get
                {
                    ClaimsPrincipal claimsPrincipal = Thread.CurrentPrincipal as ClaimsPrincipal;
                    if (claimsPrincipal == null)
                    {
                        return null;
                    }
    
                    ClaimsIdentity identity = claimsPrincipal.Identities.First();
                    if (identity == null)
                    {
                        return null;
                    }
    
                    Claim tokenClaim = identity.Claims.FirstOrDefault(c => c.Type == "User_Token");
                    if (tokenClaim == null || string.IsNullOrEmpty(tokenClaim.Value))
                    {
                        return null;
                    }
    
                    return tokenClaim.Value;
                }
                set
                {
                    ClaimsPrincipal claimsPrincipal = Thread.CurrentPrincipal as ClaimsPrincipal;
                    var identity = claimsPrincipal.Identities.First();
    
                    Claim tokenClaim = identity.Claims.FirstOrDefault(c => c.Type == "User_Token");
    
                    if (tokenClaim != null)
                    {
                        identity.RemoveClaim(tokenClaim);
                    }
    
                    identity.AddClaim(new Claim("User_Token", value));
                    var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                    authenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
                    authenticationManager.SignIn(new AuthenticationProperties { IsPersistent = true }, identity);
                }
            }
    

    Thanks

  • User Avatar
    0
    geckotechnical created

    Hi,

    Would someone be able to assist with this, please, I'm still not sure on how to accomplish custom session properties with the Angular 2/.NET Core project

    Thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    If you are using angular2 template, then you can do it in TokenAuthController just before this line <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero-core/blob/dev/aspnet-core/src/MyCompanyName.AbpZeroTemplate.Web.Core/Controllers/TokenAuthController.cs#L136">https://github.com/aspnetzero/aspnet-ze ... er.cs#L136</a>.