2012-02-11 40 views
0

此前我有類似的問題。 我已經在家庭控制器上創建了asp mvc 默認模板項目並設置了authorization屬性。 當我運行的應用程序網址是:用戶未通過身份驗證時的路由設置問題

http://localhost:48403/Account/LogOn?ReturnUrl=%2f 

什麼,我試圖讓只是http://localhost:48403當用戶沒有通過身份驗證,但我只是有reouting設置:( 我試圖把這個global.asax但沒有運氣沒有運氣:

routes.MapRoute("Login", "", 
       new { controller = "Account", action = "LogOn" } 
      ); 

這是我的整個global.asax

routes.MapRoute("About", "About", 
       new { controller = "Home", action = "About" } 
      ); 

      routes.MapRoute("Login", "", 
       new { controller = "Account", action = "LogOn" } 
      ); 

      routes.MapRoute(
       "Default", // Route name 
       "{controller}/{action}/{id}", // URL with parameters 
       new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults 
      ); 
+0

使用默認的模板,我剛剛創建了一個新的ASP.NET MVC 3應用程序,我的裝飾與授權屬性HomeController中,改變了我的航線global.asax與你的問題完全一樣,運行該應用程序,URL爲'http:// localhost:xxxx /',並顯示登錄操作。很顯然,HomeController從未執行過,因爲您已將帳戶的路由放在默認路由之前。你究竟想達到什麼目的? – 2012-02-12 08:30:09

+0

如果我刪除「登錄」路線比我的網址是'http:// localhost:xxxx/Account/LogOn?ReturnUrl =%2f'。而當它的網址是好的'http:// localhost:xxxx /'但我登錄後我無法進入HomeController。我應該更改默認網址或什麼的,我不知道 – 1110 2012-02-12 10:01:03

+0

你的目標是什麼?請描述您需要與您的網站進行互動的方式。如果你想進入HomeController的Index動作,你必須使用'/ Home/Index'而不是'/',因爲'/'將始終根據你的路由呈現登錄動作。 – 2012-02-12 10:07:48

回答

2

你可以寫一個卡斯特OM授權的屬性,它代替如果重定向將直接呈現在登錄視圖:

public class MyAuthorizeAttribute : AuthorizeAttribute 
{ 
    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) 
    { 
     var viewResult = new ViewResult 
     { 
      ViewName = "~/Views/Account/LogOn.cshtml" 
     }; 
     filterContext.Result = viewResult; 
    } 
} 

然後用它裝點您的HomeController:

[MyAuthorize] 
public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     return View(); 
    } 
} 

那麼你的AccountController就不再需要擔心ReturnUrls:

public class AccountController : Controller 
{ 
    [HttpPost] 
    public ActionResult LogOn(LogOnModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      if (Membership.ValidateUser(model.UserName, model.Password)) 
      { 
       FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); 
       // TODO: obviously here instead of hardcoding the country and the city 
       // you might want to retrieve them from your backend given the username 
       return RedirectToAction("Index", "Home", new { country = "uk", city = "london" }); 
      } 
      else 
      { 
       ModelState.AddModelError("", "The user name or password provided is incorrect."); 
      } 
     } 

     return View(model); 
    } 

    public ActionResult LogOff() 
    { 
     FormsAuthentication.SignOut(); 
     return RedirectToAction("Index", "Home"); 
    } 
} 

最後您需要修改〜/ Views/Account/LogOn.cshtml中表單的動作以指向正確的控制器:

@using (Html.BeginForm("LogOn", "Account")) { 
    ... 
} 

你可以在默認情況下離開你的路線:

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

    routes.MapRoute(
     "Default", 
     "{controller}/{action}/{id}", 
     new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
    ); 
} 
+0

謝謝。還有一件事我會弄清楚自己。當用戶沒有登錄,然後點擊about,然後登錄用戶仍然去首頁/索引,但應該去關於 – 1110 2012-02-12 10:55:13

+0

@ 1110,這正是我在你以前的評論中問你的問題,你說無論哪個網址用戶在他的地址欄中輸入,成功驗證後,他應該被重定向到「Home/Index」。顯然,由於您不想在登錄時在地址欄中使用「ReturnUrl」查詢字符串參數,LogOn控制器操作無法知道在成功登錄時必須重定向哪個url。這個參數的目的就是這個。 – 2012-02-12 17:06:09

相關問題