ASP.NET MVC项目演练:用户登录
2016-11-19 11:34
543 查看
设置默认启动页面
public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional } ); } }
设置重定向配置(没有登录的匿名用户将重定向到配置的地址)
<authentication mode="Forms"> <forms loginUrl="~/Account/Login" timeout="2880"></forms> </authentication>
设置控制器过滤器;
Authorize特性也可以只设置方法;下面的代码中,如果用户没有登录,请求Home/UserCenter话会被定向到登录界面(Account/Login)
//Authorize,过滤器(filter),禁止匿名访问 [Authorize] public class HomeController : Controller { //允许匿名用户访问 [AllowAnonymous] public ActionResult Index() { return View(); } public ActionResult UserCenter() { return View(); } }
登录数据模型,用的是VS自动生成的,可以根据自己的需求定制,包括数据验证特性,可参考http://www.cnblogs.com/liunlls/p/aspnet_mvc_adding_validation.html
public class LoginViewModel { [Required] [Display(Name = "账号")] public string Account { get; set; } [Required] [DataType(DataType.Password)] [Display(Name = "密码")] public string Password { get; set; } [Display(Name = "记住我?")] public bool RememberMe { get; set; } }
登录方法
[HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public ActionResult Login(LoginViewModel model, string returnUrl) { if (!ModelState.IsValid) { return View(model); } //验证账号密码 if (model.Account.Equals("admin") && model.Password.Equals("123456")) { string userData = new JavaScriptSerializer().Serialize(model); //验证票据 var ticket = new FormsAuthenticationTicket(1, model.Account, DateTime.Now,DateTime.Now.AddDays(COOKIE_EXPIRES), false, userData, FormsAuthentication.FormsCookiePath); //加密 string encrypt = FormsAuthentication.Encrypt(ticket); var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypt); if (model.RememberMe) { cookie.Expires = DateTime.Now.AddDays(COOKIE_EXPIRES); } //保存cookie Response.Cookies.Remove(cookie.Name); Response.Cookies.Add(cookie); if (string.IsNullOrEmpty(returnUrl)) { return RedirectToAction("Index","Home"); } else return Redirect(returnUrl); } else { ModelState.AddModelError("", "无效的登录尝试。"); return View(model); } }
注销用户方法
public ActionResult LoginOut() { FormsAuthentication.SignOut(); return Redirect(FormsAuthentication.LoginUrl); }
相关文章推荐
- asp.net -mvc框架复习(7)-基于MVC搭建用户登录项目框架
- 【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目(6) 控制器基类 主要做登录用户、权限认证、日志记录等工作
- ASP.NET MVC 2入门演练 1 - 创建项目
- 【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目 (16)源码分享二:登录功能以及UI、数据库、仓储源码分享
- VS2012 asp.net mvc 4 运行项目提示:"错误消息 401.2。: 未经授权: 服务器配置导致登录失败"
- ASP.NET Core 2.0 MVC - 获取当前登录用户信息
- C# MVC 用户登录状态判断 【C#】list 去重(转载) js 日期格式转换(转载) C#日期转换(转载) Nullable<System.DateTime>日期格式转换 (转载) Asp.Net MVC中Action跳转(转载)
- ASP.NET MVC 使用Filter过滤器 验证用户登录状态
- ASP.Net MVC Filter验证用户登录
- Asp.net mvc验证用户登录之Forms实现-转
- 【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目 登录界面前端样式和特效
- 演练:ASP.NET控件实现 创建具有成员资格和用户登录功能的网站
- ASP.NET MVC用户登录(Memcache存储用户登录信息)
- Asp.Net Core 项目实战之权限管理系统(5) 用户登录
- ASP.net MVC 中Security.FormsAuthentication验证用户的状态(匿名|已登录)
- ASP.NET MVC 用户登录Login
- Asp.net mvc验证用户登录之Forms实现详解
- ASP.NET MVC 用户登录Login
- ASP.NET MVC下判断用户登录和授权状态方法
- ASP.NET MVC下判断用户登录和授权状态方法