tags:

views:

29

answers:

1

Hello

I was wondering if there is a simple way to not use the aspnet_* tables and all that to add a login/logout function to an mvc application? I need a simple custom login+logout function, and it makes no sense to having all that functionality avalible for my app.

/M

+1  A: 

You can use something like this:

public ActionResult Login(string userName, string password, string returnUrl)
{
    if (AuthenticationHelper.Authenticate(userName, password))
    {
        if (!String.IsNullOrEmpty(returnUrl) && returnUrl != "/default.aspx")
        {
            return Redirect(returnUrl);
        }
        else
        {
            return RedirectToAction("Index", "Home");
        }
    }
}

The Authenticate method checks against the database.

Ralph Stevens
Is there some way to make it so I can put like [MyAuth] over the action results? And how would that helper look? Session something or?
molgan
In the Authenticate method, if the database check succeeds, you run the following: FormsAuthentication.SetAuthCookie(userID, false);The userID is a unique id you associate with the user. A cookie would be generated automatically by ASP.NET.
Ralph Stevens