I have been scratching my head on this for a while now but still can’t get it.
I’m trying to simply log in a user in an MVC2 application.
I have tried everything that I know to try but still can’t figure out what I’m doing wrong.
Here are a few things that I have tried:
FormsAuthentication.SetAuthCookie( emailAddress, rememberMe );
var cookie = FormsAuthentication.GetAuthCookie( emailAddress, rememberMe );
HttpContext.Response.Cookies.Add( cookie );
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( emailAddress, rememberMe, 15 );
FormsIdentity identity = new FormsIdentity( ticket );
GenericPrincipal principal = new GenericPrincipal(identity, new string[0]);
HttpContext.User = principal;
I’m not sure if any of this is the right thing to do (as it’s not working).
After setting HttpContext.User = principal then Request.IsAuthenticated == true.
However, in Global.asax I have this:
HttpCookie authenCookie = Context.Request.Cookies.Get(
FormsAuthentication.FormsCookieName );
The only cookie that ever is available is the aspnet session cookie.
Any ideas at all would be much appreciated!
You’re doing way too much work. It takes one function call to log someone in. Here’s the boilerplate code from a new MVC 2 app:
Note the commented line. That’s all you need to do a login. I note that you’re not calling
ValidateUserin your code in the question. You need that.