I would use the ASP.NET membership and role provider model. If you would like to do it with your custom tables you can create a class that inherits from Membership Provider. There are a number of methods you can implement to support things like changing passwords, forgot password etc... but the one for logging in would be ValidateUser
public sealed class MyMembershipProvider : MembershipProvider
{
public override bool ValidateUser(string username, string password)
{
bool isValid = false;
// your authentication logic here
var ticket = new FormsAuthenticationTicket(
1,
YOUR_USER_ID_HERE,
DateTime.Now,
DateTime.Now.AddMinutes(30),
false,
name,
FormsAuthentication.FormsCookiePath);
var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
HttpContext.Current.Response.Cookies.Add(authCookie);
return isValid;
}
}
You will also need to create a role provider if you would like there to be different levels of users. To do so you will inherit from the RoleProvider class.
public sealed class MyRoleProvider : RoleProvider
{
// Implement logic here
}
To authorize certain areas of your application you would use the Authorize attribute.
public class MyController : Controller
{
[Authorize(Roles="Role1,Role2")]
public ActionResult Index()
{
// Implement your code
}
}
Finally there is some configuration in the web.config you have to do to get it to use your providers.
<authentication mode="Forms">
<forms loginUrl="~/Login" timeout="2880"/>
</authentication>
<membership defaultProvider="MyMembershipProvider" userIsOnlineTimeWindow="20">
<providers>
<clear/>
<add name="MyMembershipProvider" type="Your.NameSpace.MyMembershipProvider" enablePasswordRetrieval="false" enablePasswordReset="false" requiresQuestionAndAnswer="false" writeExceptionsToEventLog="false"/>
</providers>
</membership>
<roleManager enabled="true" defaultProvider="MyRoleProvider" cacheRolesInCookie="true">
<providers>
<clear/>
<add name="MyRoleProvider" type="Your.NameSpace.MyRoleProvider"/>
</providers>
</roleManager>
You can find more information about the memberhsip and role providers on MSDN