Cybercrux

Everything is achievable through technology

Anti-forgery token

Mvc5 Antiforgery token multi tabs page error
if two login pages are open in a tab and after login in on tab some one try to login other tab it will cause a error
to sole this error use below filter


///
/// Handle Antiforgery token exception and redirect to customer area if the user is Authenticated
///

public class RedirectOnError : HandleErrorAttribute
{
///
/// Override the on exception method and check if the user is authenticated and redirect the user
/// to the customer service index otherwise continue with the base implamentation
///

///Current Exception Context of the request public override void OnException(ExceptionContext filterContext)
{
if (filterContext.Exception is HttpAntiForgeryException && filterContext.HttpContext.User.Identity.IsAuthenticated)
{
// Set response code back to normal
filterContext.HttpContext.Response.StatusCode = 200;

// Handle the exception
filterContext.ExceptionHandled = true;

UrlHelper urlH = new UrlHelper(filterContext.HttpContext.Request.RequestContext);

// Create a new request context
RequestContext rc = new RequestContext(filterContext.HttpContext, filterContext.RouteData);

// Create a new return url
string url = RouteTable.Routes.GetVirtualPath(rc, new RouteValueDictionary(new { Controller = “CustomerArea”, action = “Index” })).VirtualPath;

// Check if there is a request url
if (filterContext.HttpContext.Request.Params[“ReturnUrl”] != null && urlH.IsLocalUrl(filterContext.HttpContext.Request.Params[“ReturnUrl”]))
{
url = filterContext.HttpContext.Request.Params[“ReturnUrl”];
}

// Redirect the user back to the customer service index page
filterContext.HttpContext.Response.Redirect(url, true);
}
else
{
// Continue to the base
base.OnException(filterContext);
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s