asp.net – 来自AJAX的表单身份验证和POST请求
我们有一个受表单身份验证保护的ASP.NET应用程序该应用程序大量使用MS
AJAX来调用其Web服务.
当表单身份验证超时,并且发生GET请求时 – 一切都很好(用户被重定向到登录页面). 但是当表单身份验证超时并发生POST请求时(ajax) – 没有重定向发生,而应用程序返回“401 unathorized”,浏览器提示输入用户名和密码(不是登录表单,而是浏览器内置对话框) ).当然输入任何用户名/密码永远不会有帮助. 我该如何处理? 更新:用firebug查看之后,我发现常规POST请求重定向到登录正常,只有网络服务调用才会抛出“401 Unauthorizes”. 解决方法
好的,回答我自己的任务.
在研究了这个问题并进行了一些研究之后,我发现当一个web-app受到Forms-Authentication的保护并且用户未经过身份验证时,会发生以下情况: >如果是GET请求 – 用户是 这就是ASP.NET的工作原理 如果一个Web服务由AJAX(xmlHttpRequest对象)调用并返回401 – 当然浏览器会显示一个弹出式登录框. 现在,您应该做的是向Application_PostAuthenticateRequest添加一些代码,以防止为webservices抛出401. protected void Application_PostAuthenticateRequest(Object sender,EventArgs e) { if (Request.RequestType == "POST" //if its POST && !User.Identity.IsAuthenticated //if user NOT authed && !HasAnonymousAccess(Context) //if it's not the login page ) { //lets get the auth type Configuration config = WebConfigurationManager.OpenWebConfiguration("~"); SystemWebSectionGroup grp = (SystemWebSectionGroup)config.GetSectionGroup("system.web"); AuthenticationSection auth = grp.Authentication; //if it FORMS auth if(auth.Mode== AuthenticationMode.Forms) { //then redirect... this redirect won't work for AJAX cause xmlHttpRequest can't handle redirects,but anyway... Response.Redirect(FormsAuthentication.LoginUrl,true); Response.End(); } } } public static bool HasAnonymousAccess(HttpContext context) { return UrlAuthorizationModule.CheckUrlAccessForPrincipal( context.Request.Path,new GenericPrincipal(new GenericIdentity(string.Empty),null),context.Request.HttpMethod); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 验证失败时,TextBox周围出现红色边框
- asp.net-core – 如何在Asp.net Core中使用soap web服务?
- asp.net – 高级:HttpModule Init()方法在应用程序的生命中
- ASP.NET Core 2.0 Web API基于JWT自定义策略授权
- 在asp.net Repeater中添加多个新行
- asp.net – 有没有理由不将pdb文件部署到生产Web服务器?
- asp.net – 在TextChanged上停止回发
- asp.net – 使用sql helper时出现超时问题(Microsoft.Appli
- asp.net – 无法加载mysql.web程序集
- asp.net – 如何检查文档是否准备好?
- asp.net字符串分割函数使用方法分享
- asp.net – Web.config Build vs Release transf
- asp.net-mvc-4 – 如何使用SimpleMembership管理
- ASP.NET报告系统
- 如何创建一个asp.net会员提供者手动加密密码?
- asp.net – 在IIS7.5中不可见的无扩展名图像文件
- 如何在ASP.NET MVC应用程序中组织JavaScript代码
- asp.net – 客户端确认后DropdownList autoposba
- 用于属性的ASP.NET MVC编辑器模板
- OAuth(OAuth2)ASP.NET REST Web API(自我主机 –