asp.net-mvc – asp.net mvc并检查用户是否已登录
发布时间:2020-12-16 07:37:43 所属栏目:asp.Net 来源:网络整理
导读:我是asp.net mvc的新手,我需要检查用户是否在我的应用程序中登录,所以我将以下代码放在我的global.asax中 void Application_PreRequestHandlerExecute(object sender,EventArgs e) { HttpApplication application = (HttpApplication)sender; HttpContext co
我是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"); } } } 我拦截每个传入的请求进行检查我想知道是否有其他方法来完成这种工作 解决方法
你需要这样做吗?您应该检查,如果您可以使用asp.net身份验证,授权和成员资格提供程序. (当您创建新的ASP.NET MVC 3应用程序时[当您选中“Internet应用程序”时),它们会自动生成).
然后,您可以对控制器和操作使用注释:(伪代码): [Authorize] controller{.....} 要检查用户是否已登录,已存在具有Identity属性的User属性. controller...() { ... if (User.Identity.IsAuthenticated) ... ... } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
推荐文章
站长推荐
- 与ASP.net WEB API进行交易
- asp.net-mvc – 为一个MVC视图使用两个强类型模型
- asp.net – web.config allowDefinition = Machi
- asp.net-mvc – MVC 5渲染视图到字符串
- ASP.NET MVC使用Oauth2.0实现身份验证
- asp.net-mvc – ApplicationDbContext – 它在项
- ASP.NET中maxPageStateFieldLength的最佳值是多少
- asp.net-web-api – Web API / JsonMediaTypeFor
- 新版本 swagger 组件中 Servers 的 坑
- asp.net-mvc – 如何从ValidationAttribute中获取
热点阅读