2014-01-19 156 views
2

工作在我的應用我使用授權屬性在MVC 4

System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=2.0.0.0 

爲我的會員供應商,在這裏我的賬戶控制器代碼

[HttpPost] 
     public ActionResult Login(string username,string password) 
     { 
      var result = Membership.ValidateUser(username, password); 
      if(result) 
      { 

       var user = Membership.GetUser(username); 
       var roles = Roles.GetRolesForUser(username); 
       var isDistributor = roles.Any(x => x.ToUpper() == "DISTRIBUTOR"); 
       if (isDistributor) return RedirectToAction("ShowCurrentDistributor", "Distributor"); 
      } 
      else 
      { 
       TempData["error"] = "Invalid login attempt"; 
      } 
      return View(); 
     } 

而且我ShowCurrentDistributor動作代碼:

[HttpGet] 
     [Authorize(Roles = "Distributor")] 
     public ActionResult ShowCurrentDistributor() 
     { 

      var distributor = _distributer.GetDistributerbyEmail(User.Identity.Name); 
      return View(distributor); 
     } 

但是當我打電話ShowCurrentDistributor方法,授權不起作用。它總是返回到我的登錄表單,即使我通過有效角色(分銷商)的身份驗證。什麼錯我的代碼

+0

固定:我在後,如果加入這一行(結果) {FormsAuthentication.SetAuthCookie(username,true); 。因爲validateuser onlyc檢查用戶名和密碼是有效的,它不會設置任何身份驗證令牌 –

回答

0

您需要驗證成功後添加以下行:

FormsAuthentication.SetAuthCookie(username,true); 

(回答由原始的海報問題,評論)