In a Web API, I've created a TokenAuthentication
attribute to verifying token that has come from client and after that the user should sign in, then i use my UserServiceManager
to sign in like this:
ApplicationUser aUser = new ApplicationUser();
user.CopyToModel(aUser);
_signInManager.SignIn(aUser, true, true);
//_signInManager.UserManager.CreateIdentity(authenticationType,"")
//var l = _authenticationManager.GetExternalLoginInfo();
////l.Login.
//var authType = _authenticationManager.GetAuthenticationTypes();
//var cliamIdentity = _authenticationManager.CreateTwoFactorRememberBrowserIdentity(aUser.Id);
var claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.Name, user.UserName));
claims.Add(new Claim(ClaimTypes.Email, user.Email));
claims.Add(new Claim(ClaimTypes.NameIdentifier, user.UserName));
var identity = new ClaimsIdentity(claims, "CustomApiKeyAuth");
var principal = new ClaimsPrincipal(new[] { identity });
Thread.CurrentPrincipal = principal;
System.Web.HttpContext.Current.User = principal;
_authenticationManager.SignIn(new AuthenticationProperties { IsPersistent = true }, identity);
return UpdateResult<SignInStatus>.Success(SignInStatus.Success);
I Used both Thread.CurrentPrincipal
and System.Web.HttpContext.Current.User
to set Principal due to different thread in web api.
My purpose is the Thread.CurrentPrincipal.Identity.Name
can be use in my api controller. then i set this in my TokenAuthentication
Attribute after sign in:
Thread.CurrentPrincipal = System.Web.HttpContext.Current.User;
it doesn't work and Thread.CurrentPrincipal.Identity.Name
is still empty in api controller.I moved the above line in my action and worked but I don't want to write it in each of my actions. what should i do?