I need to write a custom cookie to the client after the user has logged in. This project is using Asp.Net Identity 2.0 and the default Visual Studio MVC 5 template so its no longer straight forward as it had been in the past.
I thought the place to do this would be inside the ApplicationUser.GenerateUserIdentityAsync() method, but HttpContext.Current is always null when this method executes and I assume its because its declared as an asynchronous Task<> and being called from a separate thread.
I also tried creating an event in the ApplicationUser class but this doesn't work either because again, its called from a separate thread. I could re-write this method to be synchronous, but I'm wondering what the correct way to do this using the out of the box template that Microsoft is providing.
public class ApplicationUser : IdentityUser<int, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
public ApplicationUser()
{
LastLogin = String.Empty;
HasLoggedInBefore = false;
UserCreated += ApplicationUser_UserCreated;
}
void ApplicationUser_UserCreated(object sender, ApplicationUser user)
{
// write our custom cookie
user.WriteDetailsCookie();
}
public event EventHandler<ApplicationUser> UserCreated;
protected virtual void OnUserCreated(ApplicationUser user)
{
EventHandler<ApplicationUser> handler = UserCreated;
if (handler != null)
{
handler(this, user);
}
}
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, int> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
OnUserCreated(this); // fire our event
return userIdentity;
}
public void WriteDetailsCookie()
{
var lastLoginCookie = new HttpCookie("UserDetails");
lastLoginCookie.Values.Add("userName", UserName);
lastLoginCookie.Values.Add("lastLogin", DateTime.UtcNow.ToString("G"));
lastLoginCookie.Expires = DateTime.Now.AddDays(90d);
HttpContext.Current.Response.Cookies.Add(lastLoginCookie);
}
}