asp.net-mvc – Mvc3 Antiforgery令牌多标签
发布时间:2020-05-24 12:26:52 所属栏目:asp.Net 来源:互联网
导读:我们在登录页面上遇到了防伪标记的特定问题.如果用户只使用一个活动窗口登录,则一切正常,但是如果用户在两个不同的窗口中打开登录页面并从窗口A登录(没有问题将登录),则返回到此窗口中的窗口B登录用户将收到“未提供所需的防伪标记或无效”. 有没有办法绕过这
|
我们在登录页面上遇到了防伪标记的特定问题.如果用户只使用一个活动窗口登录,则一切正常,但是如果用户在两个不同的窗口中打开登录页面并从窗口A登录(没有问题将登录),则返回到此窗口中的窗口B登录用户将收到“未提供所需的防伪标记或无效”. 有没有办法绕过这个从视图/控制器动作中删除防伪标记?我们更愿意使用令牌以获得额外的安全性! 这与这个问题非常相似,但要求mvc2 解决方法MVC3或MVC4中的这种行为是按照设计的,但如上所述,它对用户不友好,但是在生产中,这个问题需要优雅地解决,应用程序需要处理这种奇怪的情况.此问题的解决方案是创建一个应用于登录帖子的过滤器,该过滤器将验证用户是否已登录并将其带到正确的页面,否则它们将保留在登录页面上.下面是filter属性的代码 /// <summary>
/// Handle Antiforgery token exception and redirect to customer area if the user is Authenticated
/// </summary>
public class RedirectOnError : HandleErrorAttribute
{
/// <summary>
/// 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
/// </summary>
/// <param name="filterContext">Current Exception Context of the request</param>
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);
}
}
}
这是使用示例 [HttpPost]
**[RedirectOnError]**
[ValidateAntiForgeryToken]
public ActionResult LogOn(LogOnViewModel model,UserSessionState session,string returnUrl)
{
.....
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 如何在ASP.NET中动态生成列表项到无序列表?
- asp.net – 为什么Visual Studio会拒绝访问,尝试使用NuGet软
- asp.net-mvc-3 – 在使用Unity容器时为此对象异常定义的无参
- asp.net-mvc – ASP.NET MVC 2 on mono
- asp.net – 如何在mvc3中对来自@ Html.LabelFor()的内容进行
- asp.net – 如何在.net中读取Elastic Beanstalk环境属性?
- asp.net web表单json返回结果
- asp.net-mvc – 将Castle Windsor与SignalR集成 – 我该如何
- asp.net – MSBuild:自动收集db迁移脚本?
- asp.net-mvc – ASP.NET MVC单元测试控制器与HttpContext
推荐文章
站长推荐
- asp.net-mvc – Razor视图引擎优于其他视图引擎的
- asp.net – Visual Studio 2012:无法附加进程.已
- asp.net-mvc – Sitecore MVC – 如何在页面上处
- asp.net-mvc – 具有区域的Url.Action()返回空字
- asp.net-mvc – 单选按钮如何与asp.net mvc绑定一
- asp.net – Linq-to-SQL和DateTime的奇怪
- asp.net-mvc – 在ASP.NET MVC中对ViewModels进行
- asp.net-mvc-3 – 在MVC3中的WebGrid列中应用特定
- asp.net-mvc – 在每个网址的末尾添加尾部斜杠?
- ASP.Net Checkbox值在回发错误?
热点阅读
