2011-07-23 11 views
0

私はASP.NET MVC 3アプリケーションを開発中で、すべてのログイン機能を処理するためにフォーム認証を使用しています。私は3つのユーザー役割 - マネージャー、クライアント、チューター - を作成し、ユーザーは役割に応じて異なるビューに誘導されます。役割をコーディングする前に、私のログインはうまくいきました。私のコントローラのコードは次のとおりです。システムに入る前にログイン資格情報を2回入力する必要があります - MVC3

public class AccountController : Controller 
{ 

    // 
    // GET: /Account/LogOn 

    public ActionResult LogOn() 
    { 
     return View(); 
    } 


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

    [Authorize(Roles = "Tutors")] 
    public ActionResult TutorMain() 
    { 
     return View(); 
    } 

    [Authorize(Roles = "Clients")] 
    public ActionResult ClientMain() 
    { 
     return View(); 
    } 

    // 
    // POST: /Account/LogOn 

    [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 (User.IsInRole("Manager")) 
        return RedirectToAction("Index", "Account"); 

        if (User.IsInRole("Tutors")) 
        return RedirectToAction("TutorMain", "Account"); 

        if (User.IsInRole("Clients")) 
        return RedirectToAction("ClientMain", "Account"); 
       } 
      } 
      else 
      { 
       ModelState.AddModelError("", "The user name or password provided is incorrect."); 
      } 
     } 

     // If we got this far, something failed, redisplay form 
     return View(model); 
    } 

なぜこれが起こっているのか理解してください。

おかげで、 エイミー

答えて

0

は、あなたがしようとした場合、あなたは「マネージャー」の唯一のメンバーであるすなわち場合、あなたは再びプロンプトが表示されます....あなたのログインは、関連するすべてのロールのメンバーであることを確認してください「チューター」の役割を必要とするTutorMain()を表示します。

あなたはそうのような各コントローラの複数のロールを指定することができます。

[Authorize(Roles="Manager")] 
public ActionResult Index() 
{ 
... 
} 

[Authorize(Roles = "Manager, Tutors")] 
public ActionResult TutorMain() 
{ 
     return View(); 
} 

これが面倒になった場合は、「カスタム」承認の属性を調査 - asp.net mvc Adding to the AUTHORIZE attribute

で説明したように
関連する問題