2011-03-18 62 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); 
}