Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Single-Sign-On with ASP.NET Identity 2.0 and MVC 5

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.

like image 901
N1njaB0b Avatar asked Oct 19 '22 12:10

N1njaB0b


1 Answers

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);
like image 81
N1njaB0b Avatar answered Oct 23 '22 01:10

N1njaB0b