Asp.net Mvc自定义机制处理未授权请求
我希望安全控制器(或操作)遵循以下行为
对于我的网站,如果用户发出正常请求重定向到登录页面(我可以轻松做到),
如果请求是 Ajax 类型 Request.IsAjaxRequest()==true,
,返回状态代码 401
我如何为此创建过滤器?
For my website i want following behaviors for secured controller(or action)
if a user makes a normal request redirect to login page (which i have easily able to do)
if request is Ajax type Request.IsAjaxRequest()==true
, return status code 401
How can i create a filter for this??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
创建一个像上面这样的过滤器,如果通过ajax发出请求,它将返回未经授权的请求状态代码401。
如果您使用 jQuery,您可以执行以下操作
Create a filter like above, it will return status code 401 for unauthorized request if request is made thru ajax.
If you are using jQuery you can do as below
除了接受的答案之外,我还需要放入这行代码以防止 FormsAuthentication 重定向到登录页面。filterContext.HttpContext.Response.SuppressFormsAuthenticationRedirect
= true;
然后我删除了
filterContext.HttpContext.Response.End();
In addition to the accepted answer, I needed to put this line of code in to prevent FormsAuthentication from redirecting to the login page..
filterContext.HttpContext.Response.SuppressFormsAuthenticationRedirect = true;
I then removed
filterContext.HttpContext.Response.End();
您的问题不在于 AJAX 请求,您的问题是返回 HTTP 401 未经授权的响应,因为您使用表单身份验证。此响应代码告诉框架它应该使用 HTTP 302 响应将用户代理重定向到您的登录页面。这就是为什么很容易设置“正常”请求重定向 - 它是自动完成的。
为了回答你的问题,我遇到了类似的问题,我最终得到的解决方案是不使用表单身份验证。我实现了一个自定义授权属性,可以手动处理这两种情况。我不确定这是否是最好的方法,但它确实有效。我感兴趣的是其他人对此解决方案的看法或还有哪些其他解决方案。
幸运的是,您仍然可以使用
FormsAuthentication
类来处理 cookie,但您必须从 Web.config 文件中删除表单身份验证配置。当用户登录时,您可以使用 FormsAuthentication.SetAuthCookie 来设置 cookie(您可能已经这样做了)。其次,在授权属性中,您从请求中获取 cookie,并使用 FormsAuthentication.Decrypt 对其进行解密。如果它存在并且有效,您可以根据此 cookie 在HttpContext
中设置用户,因为表单身份验证将不再为您执行此操作。如果没有,您要么重定向到登录页面,要么返回 401,具体取决于它是否是 AJAX 调用。Your problem is not with AJAX request, your problem is returning HTTP 401 Unauthorized response, because you use forms authentication. This response code tells the framework that it should redirect the user-agent to your login page with a HTTP 302 response instead. That's why it was easy to setup the "normal" request redirect - it's done automatically.
To answer your question, I had similar problem and the solution I ended up with was not using forms authentication. I implemented a custom authorization attribute that handles both cases manually instead. I'm not sure if this is the best approach, but it does work. I'm interested in what others think of this solution or what other solutions there are.
Fortunately, you can still use the
FormsAuthentication
class to handle cookies for you, but you have to delete the forms authentication configuration from your Web.config file. When the user logs in you useFormsAuthentication.SetAuthCookie
to, well, set a cookie (you are probably doing this already). Second, in your authorization attribute, you get the cookie from the request and useFormsAuthentication.Decrypt
to decrypt it. If it exists and is valid, you set the user in theHttpContext
based on this cookie, because forms authentication won't do it for you anymore. If it doesn't you either redirect to the login page or return 401, depending on whether it's an AJAX call or not.您可以使用 ajaxonly 来限制访问ajax操作结果
You can use ajaxonly to restrain access to ajax actionresult
您可以只返回 HttpUnauthorizedResult。
注意:这可能会导致 MVC 框架将您返回到登录页面。
You can just return a HttpUnauthorizedResult.
Note: This could cause the MVC framework to return you to the login page.
一个简单的方法是在 SignIn 操作中进行检查
a simple way is to do a check in the SignIn action