【问题标题】:Asp.net MVC ReturnUrl value always nullAsp.net MVC ReturnUrl 值始终为空
【发布时间】:2024-04-29 05:45:02
【问题描述】:

好的,在发布此之前,我已经研究并尝试了每一个建议(当然是单独的),但我每次都碰壁

这是我的登录视图,我使用 ViewBag 传递 ReturnUrl 值,正如我在这个问题的许多答案中看到的那样

   <h2>Login</h2>
@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { role = "form" }))
{
    @Html.AntiForgeryToken()


  ...............

这是登录操作结果

   [HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(UserLogin login, string returnUrl="")
{
    string message = "";
    using (NerdsContext nc = new NerdsContext())
    {
        var v = nc.User.Where(a => a.email == login.email).FirstOrDefault();
        if (v != null)
        {
            if (!v.IsEmailVerified)
            {
                ViewBag.Message = "Please verify your email first";
                return View();
            }
            if (string.Compare(Crypto.Hash(login.password), v.password) == 0)
            {
                int timeout = login.rememberMe ? 525600 : 20; // 525600 min = 1 year
                var ticket = new FormsAuthenticationTicket(login.email, login.rememberMe, timeout);
                string encrypted = FormsAuthentication.Encrypt(ticket);
                var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
                cookie.Expires = DateTime.Now.AddMinutes(timeout);
                cookie.HttpOnly = true;
                Response.Cookies.Add(cookie);

                //Redirect the user to new url
                if (Url.IsLocalUrl(returnUrl))
                {
                    ViewBag.ReturnUrl = returnUrl;
                    return Redirect(returnUrl);

                }
                else
                {
                    return RedirectToAction("Nerd", "Home");
                }
            }
            else
            {
                message = "Invalid credential provided";
            }
        }
        else
        {
            message = "Invalid credential provided";
        }
    }
    ViewBag.Message = message;
    return View();
}

最后这是我在 web.config 文件中添加的行

 <authentication mode="Forms">
      <forms cookieless="UseCookies"  loginUrl="/Account/Login"  timeout="30" slidingExpiration="true" protection="All"></forms>
    </authentication>

当我运行它时,我从来没有真正登录过它总是把我送回登录页面,returnUrl 的值总是为空 那么这里发生了什么????

【问题讨论】:

  • 能否包含Login GET 方法(您当前的示例只有POST 方法)?如果 GET 操作方法结果分配了 URL,则 ViewBag.ReturnUrl 值存在,否则它为 null 作为 ViewBag 默认值。如果您确定从查询字符串给出的返回 URL,请考虑使用 ViewBag.ReturnUrl = Request.QueryString["ReturnUrl"]
  • 是的,我有 GET 操作方法 public ActionResult Login(string ReturnUrl) { ViewBag.ReturnUrl = ReturnUrl; return View(); } 我会试试这个,但我必须更改视图中的任何内容吗?
  • 它没有用 :( 任何其他想法?我在这里迷失了几天,仍然没有找到解决这个令人沮丧的问题的任何方法
  • 为什么不在UserLogin viewmodel 类中添加返回 URL 属性并通过HiddenFor 传递返回的 URL?当UserLogin使用POST提交时,它会自动传递返回的URL值,你可以用ViewBag.ReturnUrl = login.returnUrl赋值。
  • 好的,现在有一个进展,返回 URL 有一个值,但我的用户仍然被重定向到登录页面,就好像他没有授予身份验证一样......我认为部分问题是缺乏代表我完全理解,我在另一个论坛上发布了这个,有人指出我没有用户实际登录的代码你还有其他想法吗?谢谢

标签: asp.net authentication model-view-controller login returnurl


【解决方案1】:

好的,经过大量搜索,我在这里找到了答案 [Request.IsAuthenticated is always false ]

我必须在 system.WebServer 内的 web.config 模块中添加这些行

<remove name="FormsAuthentication" />
      <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />

【讨论】:

  • web.config的模块在哪里或者你指的是什么文件?
  • 在web.config文件中有modules标签,里面有system.WebServer标签,我把上面的代码放在那里。