ASP.NET MVC3 实现用户登录验证
2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址:https://cloud.tencent.com/act/cps/redirect?redirect=1062
2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址:https://www.aliyun.com/minisite/goods
最近我们要做一个仿sina的微博,碰巧的是我最近在学习mvc,就想用mvc技术实现这个项目。 既然是微博,那不用想也应该知道肯定要有用户登陆,但是和常规的asp.net
自定义一个授权筛选器类,继承于AuthorizeAttribute:
推荐:ASP.NET MVC3的验证(Validation)框架
在ASP.NET MVC3的验证框架比MVC2要改进不少,下面做下简单的介绍: Server端验证 自定义Server端的validation属性,需要继承自ValidationAttribute,主要方法就
using System; using System.Web; using System.Web.Mvc; namespace MvcApplication1 { public class DWAuthorizeAttribute : AuthorizeAttribute { /// <summary> /// 判断用户是否登录 /// </summary> /// <param name="filterContext"></param> public override void OnAuthorization(AuthorizationContext filterContext) { HttpContextBase context = filterContext.HttpContext; if (context.Session["LoginUser"] == null) context.Response.Redirect("~/User/Login"); } } }
在需进行登录验证的Action上加约束:
[DWAuthorize] public ActionResult Index() { return View(); }
最近我们要做一个仿sina的微博,碰巧的是我最近在学习mvc,就想用mvc技术实现这个项目。 既然是微博,那不用想也应该知道肯定要有用户登陆,但是和常规的asp.net
相关阅读排行
- 1在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接
- 2asp.net发布到IIS中出现错误:处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”
- 3MVC页面重定向'页面跳转
- 4入侵网站服务器的8种方法
- 5mvc与三层结构终极区别