一、
首先配置webconfig,再system.web节点中添加
<authentication mode="Forms"> <forms loginUrl="/Home/Login" timeout="120" cookieless="UseCookies" name="LoginCookieName"></forms> </authentication>
其中LoginUrl为验证失败后跳转到的地址
二、
在FilterConfig里注册全局过滤器,用户通过授权方可访问控制器
filters.Add(new System.Web.Mvc.AuthorizeAttribute());
三、
登录成功后注册cookis
FormsAuthentication.SetAuthCookie(userEntity.AccountName, true);
四、
在不需要验证的控制器前加上 [AllowAnonymous],即可绕过用户登录验证,允许匿名用户访问