Reputation: 707
My challenge is to redirect a user to their Area based on their Role I placed an arrow to redirect to one area but I need to place an exception to handle another area if Role is not this Role type.
How would I modify the default LogOn Controller Action:
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home", new { area = "Client" });
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
Upvotes: 0
Views: 1310
Reputation: 5843
This is what you might be looking for:
[Authorize(Users="Smith, Steve", Roles="Admin, PowerUser")]
Users : Comma-separated list of usernames that are allowed to access the action method.
Roles : Comma-separated list of role names. To Access the action method, users must be in at least one of these roles.
http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute.aspx
Upvotes: 0
Reputation: 707
I figured it out. There were absolutely NO posts out there with this straight forward answer so feel free to repost this:
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return Redirect(returnUrl);
}
else
{
if (Roles.IsUserInRole(model.UserName, "UserRoleOne"))
{
return RedirectToAction("Index", "Home", new { area = "AreaForUserRoleOne" });
}
else
{
if (Roles.IsUserInRole(model.UserName, "UserRoleTwo"))
{
return RedirectToAction("Index", "Home", new { area = "AreaForUserRoleTwo" });
}
}
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
Upvotes: 1