Hi All,
I am trying to implement Asp.net Identity in my web (MVC5) application. In User Authentication method I am authenticating user, if user exist then I am manually creating Claims Identity and Principal in my application (Authentication.cs). After that I am assigning the Claims Principal to current HttpContext and Thread. So now I am able to see Identity in the same request httpcontext. But if I am trying to access Claims Identity from different action/page then it will show a null values in the User.Identity object, that is, Identity is not maintaing in the subsequent request.
In my application I set AuthenticationMode="None" in web.config so I removed LoginPath attribute from OWIN Startup class.
[assembly: OwinStartup(typeof(Web.Startup))]
namespace Web
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
CookieSecure = CookieSecureOption.Always
});
}
}
}
if (user != null)
{
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
var claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.Name, "username"));
claims.Add(new Claim(ClaimTypes.Email, "username@gmail.com"));
var userIdentity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);
ClaimsPrincipal principal2 = new ClaimsPrincipal(userIdentity);
authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = true }, userIdentity);
var test = HttpContext.Current.User.Identity.IsAuthenticated;
System.Threading.Thread.CurrentPrincipal = principal2;
HttpContext.Current.User = principal2;
test = userIdentity.IsAuthenticated;
test = HttpContext.Current.User.Identity.IsAuthenticated;
}
I am not sure why Identity values are not maintaining in httpcontext or can I access the identity in some other ways in other pages/actions?
I assume that I missed something in the identity implementation. :(
Thanks
Selvakumar R