[英]ASP.NET MVC 3 - Get Controller and Action and Perform Redirect from Application_OnAuthenticateRequest Method
I have implemented the Application_OnAuthenticateRequest method in my Global.asax class in my ASP.NET MVC 3 application. 我已经在ASP.NET MVC 3应用程序的Global.asax类中实现了Application_OnAuthenticateRequest方法。 When a user hits a page on my application, I load the user and verify their account status. 当用户点击我的应用程序上的页面时,我会加载该用户并验证其帐户状态。 When their account status does not match the valid account statuses, I want to redirect that user to a controller/action to perform additional actions. 当他们的帐户状态与有效的帐户状态不匹配时,我想将该用户重定向到控制器/操作以执行其他操作。
The problem I am having is that I need to get the controller/action from the request to ensure that an infinite loop of redirects doesn't occur. 我遇到的问题是我需要从请求中获取控制器/操作,以确保不会发生无限循环的重定向。 I also am not sure of the best way to handle the redirect in this method to my new controller/action. 我也不确定在此方法中重定向到新控制器/动作的最佳方法。
Any way to do this? 有什么办法吗?
It is better to put your logic inside a custom Authroze Filter instead of Application_OnAuthenticateRequest method. 最好将逻辑放在自定义的Authroze筛选器中,而不是Application_OnAuthenticateRequest方法。 Check this link 检查此链接
在您的控制器中,可以将[Authorize]放在您的方法签名之前,如果未经授权,这会将用户重定向到另一个视图。
How about a Custom Method to authorize the user and Try/Catch? 如何使用自定义方法授权用户并进行“尝试/捕获”? In "Catch" send him to the View you want. 在“捕获”中,将他发送到所需的视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.