我们有一个新项目,目前还很简单。
我正在设置表单验证并有以下 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/