2012-06-30 47 views
0

我的挑战是用户根据自己的角色 他们的区域,我把箭头重定向到一个区域重定向,但我需要放置一个异常处理,如果作用的另一个领域不是这个角色类型。MVC3 - 基于角色的用户登录并重定向

我怎么会修改默认登录控制器动作:

[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); 
    } 
+0

您可以用'如果(User.IsInRole( “管理”))'之类的这个东西。 –

+0

对不起,我错了解释,只是说,如果你看一下实际的代码,我把视觉箭头在那里我重定向用户到一个区域...... ============> –

回答

1

我想通了。有绝对没有职位在那里与此直线前进答案可以随意转载当:

 [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."); 
      } 
     } 
+0

很难相信没有其他人还需要这个。感谢你的回答。 – HendPro12