gpt4 book ai didi

c# - 在已经登录的情况下处理登录期间的防伪造错误? ASP.NET MVC

转载 作者:可可西里 更新时间:2023-11-01 09:03:03 28 4
gpt4 key购买 nike

当用户登录时,并转到登录页面。如果他再次尝试登录,您将收到防伪错误。

The anti-forgery token could not be decrypted. If this application is hosted by a Web Farm or cluster, ensure that all machines are running the same version of ASP.NET Web Pages and that the configuration specifies explicit encryption and validation keys. AutoGenerate cannot be used in a cluster.

我得到的另一种错误是:

The provided anti-forgery token was meant for a different claims-based user than the current user.

如何处理这个防伪错误?

最佳答案

创建操作过滤器继承 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...
}

编辑评论

使用另一个 Controller / Action 作为 AlreadySignIn()

Controller 代码

public ActionResult AlreadySignIn()
{
return View();
}

Razor 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>

希望这对您有所帮助。

关于c# - 在已经登录的情况下处理登录期间的防伪造错误? ASP.NET MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25847432/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com