2016-09-01 127 views
0

在我的賬戶控制器,在登錄操作,有下面的代碼:MVC5(Asp.Net4.5.2) - RedirectToAction在一種情況下一點兒也不回

   case "Sucess": 
        string rule = CheckRule(model.username, model.Password); 
        Response.SetCookie(SetAuthCookie(model.username, model.RememberMe, rule)); 
        return RedirectToAction("Index", rule); 

在checkrule,我返回字符串的名字另一個控制器,其中有管理員和BasicUser,這裏是這些代碼:

{ 
[Authorize] 
public class AdminController : Controller 
{ 
    private bool attAuthor = isAuthorized(); 
    private bool attAuth = isAuthenticated(); 
    private string rule = returnrule(); 
    // GET: Admin 
    public ActionResult Index() 
    { 
     if (!attAuthor) 
     { 
      return RedirectToAction("erro401",rule); 
     } 
     else 
     { 
      return View(); 
     } 


    } 

    public ActionResult erro401() 
    { 
     return View("erro401"); 
    } 

}

和:

{ 
[Authorize] 
public class BasicUserController : Controller 
{ 
    private bool attAuthor = isAuthorized(); 
    private bool attAuth = isAuthenticated(); 
    private string rule = returnrule(); 
    // GET: BasicUser 
    public ActionResult Index() 
    { 
     if (!attAuthor) 
     { 
      return RedirectToAction("erro401", rule); 
     } 
     else 
     { 
      FormsAuthenticationTicket authticket = get_ticket(); 
      string str = rule + "/" + authticket.Name; 
      ViewBag.Htmlstr = str; 
      return View(); 
     } 


    } 

    public ActionResult erro401() 
    { 
     return View("erro401"); 
    } 


} 

}

在RouteConfig:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapRoute(
      name: "Default", 
      url: "", 
      defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional } 
     ); 

     routes.MapRoute(
      name: "BasicUser", 
      url: "{controller}/{action}/{id}", 
      defaults: new { controller = "BasicUser", action = "Index", id = UrlParameter.Optional } 
     ); 
     routes.MapRoute(
      name: "Admin", 
      url: "{controller}/{action}/{id}", 
      defaults: new { controller = "Admin", action = "Index", id = UrlParameter.Optional } 
     ); 

如果我和它完美的管理員用戶登錄,但有一個基本的用戶navagador不重定向到的路線,簡單地說就是登錄屏幕上,如果我輸入控制器地址的作品。 我添加了一個標籤html來查看cookie userdata,這工作正常(顯示的BasicUserRule)。

很抱歉,如果我的問題不是很清楚,我是新手...

回答

1

有兩個問題與你的路線:

  1. 我不建議映射「」登錄。這實際上是首頁。將主頁映射到登錄沒有多大意義。當您從任何頁面,屬性返回HttpStatusCode.Unauthorized響應時,應自動請求您的登錄頁面。如果您希望只有授權用戶才能訪問您的主頁,請返回來自主頁的未經授權的回覆。就是這樣。

    這是一個非常好的主意,詳細瞭解MVC(路由,控制器,認證,授權)是如何工作的。否則,你可能會得到一個遠離安全的應用程序。 StackOverflow對解決個別問題很有幫助,但無助於您理解大局。你仍然需要了解東西的工作原理。

  2. 當兩個模式完全相同時,只有第一個匹配,因爲MVC在找到匹配時會停止。您的默認值在這裏沒有幫助。你需要自行定義狀花紋:

    "Admin/{action}/{id}", new { controller = "Admin" } 
    "Basic/{action}/{id}", new { controller = "Basic" } 
    

    所以如果URL以「基本法」開始,管理員將無法匹配,反之亦然。

您可以使用Route Debugger瞭解路由匹配的工作方式以及您的要求得到如何映射到路線。

+0

塞達特您好,感謝的答案,我會解決這些路線,但'HttpStatusCode.Unauthorized',在那裏我可以配置此帳戶登錄自動響應?我把登錄URL放在Web.config上的FormsAuthentication中:'

+0

@RogérioFerreira應該足夠了。這是默認行爲。如果您想在操作前強制登錄,您還可以使用'[Authorize]'屬性(而不是處理HTTP結果)。 –

+1

謝謝,我已經做到了。 –

0

我認爲不需要你MapRoutes因爲你只是TE重定向到一個視圖。因爲兩個MapRoutes具有相同的結構,它將只保留最後一個是Admin的結構。

2

謝謝塞達特!

我的煩惱是路線,我設定控制器德路線URL參數:

routes.MapRoute(
       name: "BasicUser", 
       url: "BasicUser/{action}/", 
       defaults: new { controller = "BasicUser", action = "Index" } 
      ); 
      routes.MapRoute(
       name: "Admin", 
       url: "Admin/{action}/", 
       defaults: new { controller = "Admin", action = "Index"} 
      ); 

這解決了一切。 我跟着他的建議,並要求提供更多資訊,這裏有一些有趣的鏈接:

MVC Routing with different Areas with multiple controllers

Customizing-Routes-in-ASP-NET-MVC

(PT-BR)trabalhando-com-rotas-no-aspnet-mvc.aspx

+0

我看到了我的應用程序的其他解決方案,我將不得不重做一些事情。 –

相關問題