当用户登录时,并在登录时转到登录页。如果他试图再次登录,你将得到反伪造错误。
防伪令牌无法解密。如果此应用程序由网站场或群集承载,请确保所有计算机都运行相同版本的ASP.NET网页,并且配置指定显式加密和验证密钥。AutoGenerate不能在集群中使用。
我遇到的另一种错误是:
所提供的防伪造令牌用于与当前用户不同的基于索赔的用户。
如何处理这个防伪错误?
发布于 2014-09-15 11:58:03
创建继承HandleErrorAttribute的动作筛选器,如下例所示。然后您可以检查请求并处理错误。
public class AntiForgeryHandleErrorAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext context)
{
if (context.Exception is HttpAntiForgeryException)
{
var url = string.Empty;
if (!context.HttpContext.User.Identity.IsAuthenticated)
{
var requestContext = new RequestContext(context.HttpContext, context.RouteData);
url = RouteTable.Routes.GetVirtualPath(requestContext, new RouteValueDictionary(new {Controller = "User", action = "Login"})).VirtualPath;
}
else
{
context.HttpContext.Response.StatusCode = 200;
context.ExceptionHandled = true;
url = GetRedirectUrl(context);
}
context.HttpContext.Response.Redirect(url, true);
}
else
{
base.OnException(context);
}
}
private string GetRedirectUrl(ExceptionContext context)
{
try
{
var requestContext = new RequestContext(context.HttpContext, context.RouteData);
var url = RouteTable.Routes.GetVirtualPath(requestContext, new RouteValueDictionary(new { Controller = "User", action = "AlreadySignIn" })).VirtualPath;
return url;
}
catch (Exception)
{
throw new NullReferenceException();
}
}
}这是我的例子,请记住,您必须处理重定向部分取决于您的请求和要求。
然后登录
[HttpPost]
[AllowAnonymous]
[AntiForgeryHandleError]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(UserLoginViewModel model, string returnUrl)
{
//Your code...
}编辑的评论
使用另一个控制器/动作作为AlreadySignIn()
控制器码
public ActionResult AlreadySignIn()
{
return View();
}剃须刀视图
@using Microsoft.AspNet.Identity
@{
ViewBag.Title = "Switch Accounts";
Layout = "~/Views/Shared/_LayoutLoginRegister.cshtml";
}
<div class="col-md-12">
<div class="block-flat text-center" style="padding: 20px; margin-bottom: 0; padding-bottom: 0;">
<i class="glyphicon glyphicon-user"></i>
<br />
<label style="padding-bottom: 10px; padding-top: 10px">You're already signed in as <strong>@User.Identity.Name</strong></label>
<label style="padding-bottom: 5px; padding-top: 5px">@Html.ActionLink("Remain signed in with this account.", "Login", "User", routeValues: null, htmlAttributes: new { id = "loginLink" })</label>
<label style="padding-bottom: 5px; padding-top: 2px">@Html.ActionLink("Click here to sign out and sign with a different account", "LogOff", "User", routeValues: null, htmlAttributes: new { id = "loginLink" })</label>
</div>
</div>希望这能有所帮助。
https://stackoverflow.com/questions/25847432
复制相似问题