Can't log in a user in MVC!

Posted by devlife on Stack Overflow See other posts from Stack Overflow or by devlife
Published on 2010-01-07T04:34:12Z Indexed on 2010/05/15 23:10 UTC
Read the original article Hit count: 425

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!

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about forms-authentication