2011-03-18 111 views
1

我目前正在开发使用MVC 3一个应用程序...现在我卡在登录的角色......登录ASP.NET MVC3

如何创建登录的网站有不同的角色?我的意思是当管理员登录时,我想重定向到后端页面,以及何时它是一个普通用户重定向到该网站。我已经设置为管理员这是确定的角色,而不是为用户...

[Authorize(Roles = "user")] 
    public ActionResult Index() 
    { 
     return View(); 
    } 

    [Authorize(Roles = "Administrators")] 
    public ActionResult view() 
    { 
     return View(); 
    } 

如何在帐户控制,我需要设置重定向页面或怎么样?

回答

8

在你可以重定向到根据用户名对应的视图登录方法:

[HttpPost] 
public ActionResult LogOn(LogOnViewModel model) 
{ 
    if (ModelState.IsValid) 
    { 
     if (MembershipService.ValidateUser(model.Username, model.Password)) 
     { 
      FormsService.SignIn(model.Username, false); 
      if (string.Equals("admin", model.Username)) 
      { 
       // If the admin user logged in 
       // redirect to a different action 
       return RedirectToAction("View", "Home"); 
      } 
      return RedirectToAction("Index", "Home"); 
     } 
     else 
     { 
      ModelState.AddModelError("", "The user name or password provided is incorrect."); 
     } 
    } 
    return View(model); 
}