The easiest way is to use asp.net user names as role names.
You can write your own authorizarion attribute to handle authorization:
public class CustomAuthorizationAttribute:AuthorizeAttribute
{
public CustomAuthorizationAttribute():base()
{
Users = "registereduser";
}
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
//You must check if the user has logged in and return true if he did that.
return (bool)(httpContext.Session["started"]??false);
}
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
filterContext.HttpContext.Response.Redirect("SessionManagement/Index/?returningURL=" +
filterContext.HttpContext.Server.UrlEncode(filterContext.HttpContext.Request.Url.ToString()));
}
}
The code must handle the AuthorizeCore to return true if the user has started the session, and HandleUnauthorizedRequest to redirect the user to the login page (optionaly you can attach the returning url).
In then controller methods that need authorization, set the attribute over them:
public class SecretPageController {
[CustomAuthorizationAttribute]
ActionResult Index() {
//Method that requires authorization
return View();
}
}
Also set the authorization method to "Forms" in the web config.
Web.config:
<authentication>
<forms timeout="120"></forms>
</authentication>
Controller:
public SessionManagementController:Controller {
public ActionResult Index(string returningURL)
{
return View("Index", new SessionModel() { ReturningURL = returningURL});
}
[HttpPost]
public ActionResult Index(SessionModel mod)
{
if (UserAuthenticated(mod.UserName, mod.Password))
{
FormsAuthentication.SetAuthCookie("registereduser", false);
if (mod.UrlRetorno != null)
{
return Redirect(mod.ReturningURL);
}
return RedirectToAction("Index", "StartPage");
}
mod.Error = "Wrong User Name or Password";
return View(mod);
}
bool UserAuthenticated(string userName, string password) {
//Write here the authentication code (it can be from a database, predefined users,, etc)
return true;
}
public ActionResult FinishSession()
{
HttpContext.Session.Clear();//Clear the session information
FormsAuthentication.SignOut();
return View(new NotificacionModel() { Message = "Session Finished", URL = Request.Url.ToString() });
}
}
In the Controller, when the user enters its user name and password, set the forms authentication cookie to TRUE (FormsAuthentication.SetAuthCookie("registereduser",true)), signaling the user name (registereduser in the example) to be authenticathed. Then the user signs out, tell ASP.NET to do so calling FormsAuthentication.SignOut().
Model:
class SessionModel {
public string UserName {get;set;}
public string Password {get;set;}
public string Error {get;set;}
}
Use a model to store the user data.
View (that presents the SessionModel type):
<div class="editor-label">
<%: Html.LabelFor(model => model.UserName) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.UserName) %>
<%: Html.ValidationMessageFor(model => model.UserName) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Password) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Password) %>
<%: Html.ValidationMessageFor(model => model.Password) %>
</div>
<div class="field-validation-error"><%:Model==null?"":Model.Error??"" %></div>
<%:Html.HiddenFor(model=>model.ReturningURL) %>
<input type="submit" value="Log In" />
Use a view to get the data. In this example, there is a hidden field to store the returning URL
I hope this helps (I had to translate the code, so I'm not sure if it is 100% correct).