asp.net-mvc - ASP.NET MVC 表单例份验证导致登录 url 被调用两次

我们有一个新项目,目前还很简单。

我正在设置表单验证并有以下 View :

views/home/index.cshtml
views/logon/index.cshtml

在我的 web.config 中,我有以下内容:

<authentication mode="Forms">
  <forms loginUrl="~/LogOn/Dev" />
</authentication>

如果启用,登录操作将被调用两次。

如果我把它注释掉它只调用一次?

有什么想法吗?

最佳答案

如果没有 action 方法,有点难以分辨,但如果它看起来像这样......

[AllowAnonymous]
        public ActionResult External()
        {
            var authentication = HttpContext.GetOwinContext().Authentication;
            if (Request.HttpMethod == "POST")
            {
                foreach (var key in Request.Form.AllKeys)
                {
                    if (key.StartsWith("submit.External.") && !string.IsNullOrEmpty(Request.Form.Get(key)))
                    {
                        var authType = key.Substring("submit.External.".Length);
                        authentication.Challenge(authType);
                        return new HttpUnauthorizedResult();
                    }
                }
            }
            var identity = authentication.AuthenticateAsync("External").Result.Identity;
            if (identity != null)
            {
                authentication.SignOut("External");
                authentication.SignIn(
                    new AuthenticationProperties { IsPersistent = true },
                    new ClaimsIdentity(identity.Claims, "Application", identity.NameClaimType, identity.RoleClaimType));
                return Redirect(Request.QueryString["ReturnUrl"]);
            }

            return View();
        }

... 然后调用 HttpUnauthorizedResult() 并且此方法查找 ReturnUrl 查询字符串键/值对。如果它不存在,则它会再次循环,在第二次运行时附加当前 URL 作为该对中的值,有效地调用该方法两次。

为了避免,像这样在查询字符串中提供有效的 ReturnUrl 键值对...

<a href="/Account/Login?ReturnUrl=@ViewContext.HttpContext.Request.Url.PathAndQuery">Login to Your App</a>

注意键是区分大小写的。例如,如果您使用 ReturnURL,该方法仍将被调用两次。

https://stackoverflow.com/questions/8819260/

相关文章:

macos - 为什么 OS X 上缺少 Emacs 图标?

php - 如何将动态生成的 PDF 文件保存到 zip 文件中?

php - 发送邮件 sh :/usr/sbin/sendmail: Permission deni

image-processing - 使用小波和曲波进行特征提取

math - 执行以下 1 的补码定点整数算术运算

r - 具有大量数据的热图

macos - 从终端的钥匙串(keychain)中删除私钥

xml - Solr DataImportHandler 不适用于 XML 文件

asp.net - 我该如何解决 : the type or namespace name 'DbM

django-admin - Django 管理员覆盖字段集