views:

188

answers:

1

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!

+1  A: 

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:

    private bool ValidateLogOn(string userName, string password)
    {
        if (String.IsNullOrEmpty(userName))
        {
            ModelState.AddModelError("username", "You must specify a username.");
        }
        if (String.IsNullOrEmpty(password))
        {
            ModelState.AddModelError("password", "You must specify a password.");
        }
        if (!MembershipService.ValidateUser(userName, password)) // this is the login
        {
            ModelState.AddModelError("_FORM", "The username or password provided is incorrect.");
        }

        return ModelState.IsValid;
    }

Note the commented line. That's all you need to do a login. I note that you're not calling ValidateUser in your code in the question. You need that.

Craig Stuntz
ValidateLogOn isn't my issue.The only reason I have all of that stuff is because it wasn't working.
devlife
Ok so I have removed everything other than FormsAuthentication.SetAuthCookie( emailAddress, rememberMe );and it still doesn't work. ValidateLogOn doesn't perform the actual login. It only validates that the credentials exist in the datastore.Any other thoughts?
devlife
Read the rest of `AccountController.cs` in a default MVC project. The `LogOn` action first calls `ValidateLogOn`, Then `FormsAuth.SignIn`.
Craig Stuntz
Right but ValidateLogOn only makes sure that the credentials exist. That shouldn't have anything to do with the forms auth functionality other then for error messages. Am I wrong in that statement?
devlife
I think I may know the answer. Do you have to use a membership provider to use FormsAuthentication? If so, I definitely was not aware of this and definitely am not using a Membership provider. That is why I was so confused about why ValidateLogOn was so important.
devlife
Yes, you need a membership provider.
Craig Stuntz