I am re-writing my MVC 4 forms authentication app in MVC 5 with Identity 2.0.
Currently, the site is using a proprietary single-sign-on method that decrypts an incoming parameter from a POST request, and based on the validity of the contents, it uses FormsAuthentication.SetAuthCookie to set the authentication cookie and redirects the user to the secured content.
Now that I am using ASP.Net Identity, what is the appropriate replacement for the FormsAuthentication.SetAuthCookie functionality? Keep in mind, I don't want to have to persist any of these users to a database. They should just exist inside their tokens/cookies.
Got it! I found the solution in this helpful article.
Basically, you create a ClaimsIdentity and then use the AuthenticationManager from the OWINContext to "SignIn" the identity and that creates the authentication cookie.
Like this:
var claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.Name, "Brock"));
claims.Add(new Claim(ClaimTypes.Email, "[email protected]"));
var id = new ClaimsIdentity(claims,DefaultAuthenticationTypes.ApplicationCookie);
var ctx = Request.GetOwinContext();
var authenticationManager = ctx.Authentication;
authenticationManager.SignIn(id);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With