简体   繁体   English

ASP.NET MVC 3-获取控制器和操作并从Application_OnAuthenticateRequest方法执行重定向

[英]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.

相关问题 使用Method = POST重定向到Uri,然后从ASP.NET MVC控制器操作结果提交表单数据 - Redirect To Uri using Method=POST and submit Form Data from ASP.NET MVC Controller Action Result 从 ASP.NET MVC 中的 class 方法重定向到 Controller - Redirect to a Controller from class method in ASP.NET MVC asp.net mvc ActionFilterAttribute-重定向到“控制器/动作/参数” - asp.net mvc ActionFilterAttribute - redirect to “controller/action/parameter” 将变量从视图传递到 controller 操作方法,然后传递到同一 ASP.NET MVC controller 中的另一个方法 - Pass a variable from a view to a controller action method and then to another method in the same ASP.NET MVC controller 如何从 .net 核心 mvc 应用程序中的区域 controller 重定向到根 controller 操作方法? - How to redirect to root controller action method from an area controller in .net core mvc application? 通过带参数的JavaScript重定向到动作控制器(ASP.NET MVC) - Redirect to action controller by JavaScript with parameters (ASP.NET MVC) 如何在ASP.NET MVC 4 Web应用程序中从一个控制器的视图重定向到另一个控制器的视图 - How to redirect to a View of another Controller from one Controller's View in ASP.NET MVC 4 Web Application 向ASP.NET MVC控制器中的操作方法发出HttpPost请求 - Make HttpPost request to an action method in an ASP.NET MVC controller ASP.NET MVC AJAX 调用应用程序/控制器/操作? - ASP.NET MVC AJAX call Application/Controller/Action? 将接口传递给ASP.NET MVC Controller Action方法 - Passing an interface to an ASP.NET MVC Controller Action method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM