【问题标题】:ASP.NET MVC 3 FormsAuthentication ReturnURLASP.NET MVC 3 FormsAuthentication ReturnURL
【发布时间】:2011-07-25 11:20:24
【问题描述】:

我在我的 MVC 3 应用程序中使用表单身份验证,但我的返回 URL 有问题。

当我在 Home 控制器上标记一个动作 <Authorize> 时,它会重定向到登录页面并且可以工作,但是返回 URL 是 /,所以当它重定向时,它会重定向到当前的根目录网址Authorize

所以网址是这样的:

http://localhost/ - 控制器 = 主页 - 操作 = 索引

http://localhost/Authentication/LogOn

我最终得到了这个:http://localhost/Authentication/LogOn?ReturnURL=~%2F,我需要回到http://localhost/

救命!! :)

【问题讨论】:

  • 你的localhost默认路由是什么?
  • @Nazar - routes.MapRoute( _ "Default", _ "{controller}/{action}/{id}", _ New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional} _ )

标签: asp.net-mvc


【解决方案1】:

尝试将您的帐户控制器登录操作更改为以下内容:

[HttpPost]
    public ActionResult LogOn(LogOnModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            if (MembershipService.ValidateUser(model.UserName, model.Password))
            {
                FormsService.SignIn(model.UserName, model.RememberMe);

                return RedirectToAction("Index", "Home");

            }
            else
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
        }

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

【讨论】:

  • FormsAuthentication 处理我相信的路由。
  • 我想通了,我使用的是 RedirectToAction,应该只使用 Redirect。谢谢!!
【解决方案2】:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    • 2014-03-06
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多