我在我的登录页面上实现了防伪标记。
现在有一个用户在键盘上按下返回键,当他们填写凭据后再次点击登录按钮时,他们会得到错误页面。
是否有更好的方法处理这种情况,比如将他们重定向到新的登录页面?
登录页面是:/account/logon
如果登录细节成功,用户将被重定向到:Home / Index 页面,其中用户按下按钮返回。
我在我的登录页面上实现了防伪标记。
现在有一个用户在键盘上按下返回键,当他们填写凭据后再次点击登录按钮时,他们会得到错误页面。
是否有更好的方法处理这种情况,比如将他们重定向到新的登录页面?
登录页面是:/account/logon
如果登录细节成功,用户将被重定向到:Home / Index 页面,其中用户按下按钮返回。
不要在登录页面实现ASP.NET AntiForgeryToken。该令牌基于用户名等标准,而登录页面假定攻击者已经具有访问系统的凭据,以便能够利用该页面上的csrf。
但是,在登录页面上应该使用某种形式的CSRF保护 - 请参见https://security.stackexchange.com/a/2126/51772
private void SetANewRequestVerificationTokenManuallyInCookieAndOnTheForm()
{
if (Response == null)
return;
string cookieToken, formToken;
AntiForgery.GetTokens(null, out cookieToken, out formToken);
SetCookie("__RequestVerificationToken", cookieToken);
ViewBag.FormToken = formToken;
}
private void SetCookie(string name, string value)
{
if (Response.Cookies.AllKeys.Contains(name))
Response.Cookies[name].Value = value;
else
Response.Cookies.Add(new HttpCookie(name, value));
}
将以下代码放入您的视图中,以替代Html.AntiForgeryToken():
@if (ViewBag.FormToken != null)
{
<text><input name="__RequestVerificationToken" type="hidden" value="@ViewBag.FormToken" /></text>
}
else
{
<text>@Html.AntiForgeryToken()</text>
}
我的解决方案是:
如果再次进入登录页面,重新加载页面。 这将确保反跨站点请求伪造令牌的新加载
一切都完成了
与其他帖子提到的检查User.Identity.IsAuthenticated不同,我使用了自定义属性来处理异常,并在它是HttpAntiForgeryToken时将用户重定向到主页。
我认为这避免了使用其他方法可能存在的任何安全问题,并且[ValidateAntiForgeryToken]应始终用于POST方法。
public override void OnException(ExceptionContext filterContext)
{
var controllerName = (string)filterContext.RouteData.Values["controller"];
var actionName = (string)filterContext.RouteData.Values["action"];
var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
if (filterContext.Exception is HttpAntiForgeryException)
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{ "action", "Index" },
{ "controller", "Home" }
});
filterContext.ExceptionHandled = true;
}
}
filterContext.Result = RedirectToAction("LogOn", "Account")
filterContext.ExceptionHandled = True
End If
End Sub
- AHMED RABEE