用户未通过身份验证时的路由设置问题

问题描述:

此前我有类似的问题。 我已经在家庭控制器上创建了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

你可以写一个卡斯特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