小编ano*_*uar的帖子

asp.net mvc并检查用户是否已登录

我是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)

我拦截每个传入的请求进行检查我想知道是否还有其他方法可以做这种工作并提前感谢.

security asp.net-mvc global-asax

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

asp.net-mvc ×1

global-asax ×1

security ×1