Tạo bộ lọc hành động khi xử lý HandleErrorAttribute như ví dụ sau. Sau đó, bạn có thể kiểm tra yêu cầu và xử lý lỗi.
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();
}
}
}
Đây là ví dụ của tôi, hãy nhớ bạn phải xử lý phần chuyển hướng của bạn phụ thuộc vào yêu cầu và yêu cầu của bạn.
Sau đó đăng nhập
[HttpPost]
[AllowAnonymous]
[AntiForgeryHandleError]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(UserLoginViewModel model, string returnUrl)
{
//Your code...
}
Edited bình luận
Sử dụng một bộ điều khiển/hành động như AlreadySignIn()
điều khiển đang
public ActionResult AlreadySignIn()
{
return View();
}
Razor Xem
@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>
Hy vọng điều này sẽ hữu ích.
nó đã giải quyết được lỗi của tôi. Nhưng khi đăng nhập, phương thức này sẽ không đăng nhập vào người dùng mới. Nó sẽ chỉ hiển thị người dùng đã đăng nhập cũ. – Ruchan
Kiểm tra phần đã chỉnh sửa của câu trả lời của tôi. Nếu bạn nghĩ rằng đây là câu trả lời, vui lòng đánh dấu câu trả lời này là câu trả lời sẽ giúp người khác tìm câu trả lời cho vấn đề này. – DSR
Aww, có nghĩa là điều này thực sự không đăng nhập người dùng mới. Cảm ơn bạn, bạn đã giúp đỡ rất nhiều. – Ruchan