Click here to Skip to main content
15,849,829 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have implemented claim based authentication in asp.net core 2.0 application. but "
var identity = (ClaimsPrincipal)Thread.CurrentPrincipal;
return null spme time not always.

What I have tried:

Startup.cs:
services.AddAuthentication("TalesSecurityScheme")
                   .AddCookie("TalesSecurityScheme", options =>
                   {
                       options.AccessDeniedPath = new PathString("/Security/Access");
                       options.Cookie = new CookieBuilder
                       {
                           //Domain = "",
                           HttpOnly = true,
                           Name = ".Tales.Security.Cookie",
                           Path = "/",
                           SameSite = SameSiteMode.Lax,
                           SecurePolicy = CookieSecurePolicy.SameAsRequest
                       };

options.LoginPath = new PathString("/Login/StartPage");
                      options.ReturnUrlParameter = "RequestPath";
                      options.SlidingExpiration = true;
                  });


LoginPage:
// create claims
          List<Claim> claims = new List<Claim>
          {
              new Claim(ClaimTypes.Name, EncrypterDecrypter.Encrypt(loginEntity.UserName)),
              new Claim(ClaimTypes.Email, EncrypterDecrypter.Encrypt(loginEntity.Email)),
              new Claim(ClaimTypes.Role, loginEntity.Role),
              new Claim(ClaimTypes.Gender, EncrypterDecrypter.Encrypt(loginEntity.Gender)),                 new Claim(ClaimTypes.Sid, EncrypterDecrypter.Encrypt(loginEntity.UserId))
          };

          // create identity
          ClaimsIdentity identity = new ClaimsIdentity(claims, "cookie");


          // create principal
          ClaimsPrincipal principal = new ClaimsPrincipal(identity);

          // Set current principal
          Thread.CurrentPrincipal = principal;
          HttpContext.User = principal;

          // sign-in
          await HttpContext.SignInAsync(
                  scheme: "TalesSecurityScheme",
                  principal: principal,
                  properties: new AuthenticationProperties
                  {
                      //IsPersistent = true,
                      ExpiresUtc = DateTime.UtcNow.AddMinutes(_config.Value.CookieTimeout)
                  });


Home:
var identity = (ClaimsPrincipal)Thread.CurrentPrincipal;

          if (identity != null)
          {
              // Get the claims values
              result = identity.Claims.Where(c => c.Type == ClaimTypes.Email)
                             .Select(c => c.Value).SingleOrDefault();
          }


So SOme time returning null in
identity 
variable. Please suggest where i m doing wrong.
Posted
Updated 9-Apr-18 5:13am
v2

You can find you solution from here : ASP.NET Core 2.0 Cookie Authentication[^]
 
Share this answer
 
Comments
Pramod Singh (C) 9-Apr-18 9:13am    
Yes Dinesh, I have been implemented from same link but some time work fine and some time returning null value from (ClaimsPrincipal)Thread.CurrentPrincipal;
You can find your answer from here : [^]
 
Share this answer
 
Comments
CHill60 9-Apr-18 8:28am    
Did you know that you can use the green "Improve solution" to add more information to your post? That is better than posting multiple solutions to the same question which can be confusing
Pramod Singh (C) 9-Apr-18 9:13am    
Yes Dinesh, I have been implemented from same link but some time work fine and some time returning null value from (ClaimsPrincipal)Thread.CurrentPrincipal;
It seems like the code where you're running:
var identity = (ClaimsPrincipal)Thread.CurrentPrincipal;

is probably running on different thread than the one where you saved the claims principal.

Instead, try using ASP.NET Core's dependency injection to inject an IHttpContextAccessor. Then you could do something like this:

var context = _httpContextAccessor.HttpContext;
var user = context.User;

And once you've got the user, you can of course access all of the User's claims and do whatever you need to do with them.
 
Share this answer
 
Comments
Pramod Singh (C) 10-Apr-18 0:07am    
Yes, But i can find only user and i cant find out the role and userid and other details.
Pramod Singh (C) 10-Apr-18 0:25am    
Thanks for Quick help, Now working ... :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900