我在登录页面上实施了防伪令牌。
现在我有一个用户按下键盘上的后退键, 当他们在填写凭证后再次点击登录按钮时,他们会收到错误页面。
是否有一些更好的方式来处理这种情况,例如重定向它们 新登录页面?
登录页面是: / account / logon
如果登录详细信息已成功,则用户将被重定向到:主页/索引页面 用户按下按钮的位置。
答案 0 :(得分:20)
不要在登录页面上实现ASP.NET AntiForgeryToken。令牌基于其他条件中的用户名,登录页面假设攻击者已拥有系统凭据,以便能够在该页面上利用csrf。
但是,您应该在登录页面上使用某种形式的CSRF保护 - 请参阅https://security.stackexchange.com/a/2126/51772
答案 1 :(得分:7)
我在这里写了一个完整的解决方案:https://richardcooke.info/en/2014/keep-users-signed-in-after-asp-net-deploy/
这里是从您的GET方法控制器中调用的必要代码:
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>
}
答案 2 :(得分:5)
我的解决方案是:
如果再次点击登录页面,请重新加载页面。 这将确保防伪令牌的新装载
一切都已完成
答案 3 :(得分:0)
我没有像其他提到的那样检查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;
}
}