我是asp.net mvc的新手,我需要检查用户是否在我的应用程序中登录,所以我将以下代码放在我的global.asax中
void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
string filePath= context.Request.FilePath;
string fileExtention = VirtualPathUtility.GetExtension(filePath);
// to skip request for static content like (.css or .js)
if (fileExtention == "")
{
if (filePath.ToLower() != "/account/login")
{
var user = (Utilisateur)context.Session["USER"];
if (user == null)
context.Response.Redirect(@"~/account/login");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我拦截每个传入的请求进行检查我想知道是否还有其他方法可以做这种工作并提前感谢.