@H_419_2@验证流程
@H_419_2@一、用户登录
1、验证表单:ModelState.IsValID
2、验证用户名和密码:通过查询数据库验证
3、如果用户名和密码正确,则在客户端保存cookie以保存用户登录状态:SetAuthcookie
1):从数据库中查出用户名和一些必要的信息,并把额外信息保存到UserData中
2):把用户名和UserData保存到 FormsAuthenticationTicket 票据中
3):对票据进行加密 Encrypt
4):将加密后的票据保存到cookie发送到客户端
4、跳转到登录前的页面
5、如果登录失败,返回当前视图
@H_419_2@二、验证登录
1、在Global中注册PostAuthenticateRequest事件函数,用于解析客户端发过来的cookie数据
1):通过 httpContext.Current.User.IDentity 判断用户是否登录(FormsIDentity,IsAuthenticated,AuthenticationType)
2):从httpContext 的Request的cookie中解析出Value,解密得到 FormsAuthenticationTicket 得到UserData
2、角色验证
1):在Action加入 Authorize特性,可以进行角色验证
2):在 httpContext.Current.User 的 IsInRole 方法进行角色认证(需要重写)
@H_419_2@一、用户登录
@H_419_2@1、设置web.config
设置重定向登录页面
<system.web><authentication mode="Forms"> <forms name="loginname" loginUrl="/UserInfo/login" cookieless="Usecookies" path="/" protection="All" timeout="30"></forms></authentication></system.web>
注释掉
<modules> <!--<remove name="FormsAuthentication" />--></modules>
@H_419_2@2、登陆的验证中控制器
控制器中加“[Authorize]”修饰的方法拒绝匿名。
public class UserInfoController : Controller //控制器 { //身份验证过滤器 [Authorize] public ActionResult Index() { return VIEw(); } }
控制器中登录
/// <summary> /// 用户登录 /// </summary> /// <returns></returns> public ActionResult login() { return VIEw(); } [httpPost] public ActionResult login(loginModels login) { if (ModelState.IsValID) { var model = db.@R_301_5976@info.FirstOrDefault(a => a.@R_301_5976@Account == login.@R_301_5976@Account && a.@R_301_5976@Pwd == login.@R_301_5976@Pwd); if (model != null) { //存入票据(用户登录的时候去存信息,如果有信息直接去登录) var dtoModel = new Users { ID = model.ID, @R_301_5976@Pwd = model.@R_301_5976@Pwd, @R_301_5976@Account=model.@R_301_5976@Account }; //调用 SetAuthcookie(dtoModel); //获取@R_404_6264@ var returnUrl = Request["ReturnUrl"]; //判断@R_404_6264@是不是空值 if (!string.IsNullOrWhiteSpace(returnUrl)) { return Redirect(returnUrl); } else { //return RedirectiToAction return Redirect("/Home/index"); } } else { ModelState.AddModelError("", "账号密码不对"); return VIEw(login); } } else { ModelState.AddModelError("", "输入的信息有误"); return VIEw(login); }
对登录账号进行cookie
/// <summary> /// 对登录账号进行cookie /// </summary> /// <param name="model"></param> public voID SetAuthcookie(Users loginModel) { //1、将对象转换成Json var userdata = loginModel.ToJson(); //2、创建票据FormsAuthenticationTicket FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(2,"loginUser",DateTime.Now,DateTime.Now.AddDays(1), false, userdata); //对票据进行加密 var tickeEncrypt = FormsAuthentication.Encrypt(ticket); //创建cookie,定义 httpcookie cookie = new httpcookie(FormsAuthentication.Formscookiename, tickeEncrypt); cookie.httpOnly = true; cookie.Secure = FormsAuthentication.RequireSSL; cookie.Domain = FormsAuthentication.cookieDomain; cookie.Path = FormsAuthentication.FormscookiePath; cookie.Expires = DateTime.Now.Add(FormsAuthentication.Timeout); //先移除cookie,在添加cookie Response.cookies.Remove(FormsAuthentication.Formscookiename); Response.cookies.Add(cookie); }
@H_419_2@3、Models中添加模型文件
public class loginModels { /// <summary> /// 账号 /// </summary> [displayname("账号")] [required(ErrorMessage = "账号不能为空")] public string @R_301_5976@Account { get; set; } /// <summary> /// 密码 /// </summary> [displayname("密码")] [required(ErrorMessage = "密码不能为空")] public string @R_301_5976@Pwd { get; set; } }
@H_419_2@4、VIEws中 Login 代码:
复制代码 代码如下:
@using (HTML.BeginForm("Login", "Account", new { ReturnUrl = VIEwBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
@H_419_2@5、Global设置
protected voID Application_AuthenticateRequest(object sender, EventArgs e) { //1、通过sender获取http请求 // httpApplication app = new httpApplication();//实例化 httpApplication app = sender as httpApplication; //2、拿到http上下文 httpContext context = app.Context; //3、根据FormsAuthe,来获取cookie var cookie = context.Request.cookies[FormsAuthentication.Formscookiename]; if (cookie != null) { //获取cookie的值 var ticket = FormsAuthentication.Decrypt(cookie.Value); if (!string.IsNullOrWhiteSpace(ticket.UserData)) { //把一个字符串类别变成实体模型 var model = ticket.UserData.ToObject<@R_301_5976@infoviewmodel>(); //var acount = model.@R_301_5976@Account; //获取账号 context.User = new MyFormsPrincipal<@R_301_5976@infoviewmodel>(ticket, model); //MyFormsPrincipal.IDentity = new FormsIDentity(ticket); // MyFormsPrincipal.userdata; } } }
@H_419_2@6、退出登录
控制器中
/// <summary> /// 退出登录 /// </summary> public ActionResult loginout() { //删除票据 FormsAuthentication.SignOut(); //清除cookie Response.cookies[FormsAuthentication.Formscookiename].Expires = DateTime.Now.AddDays(-1); Response.cookies.Remove(FormsAuthentication.Formscookiename); return RedirectToAction("Index", "Home"); }
VIEw跳转链接
@HTML.Actionlink("安全退出","loginout","Users")总结
以上是内存溢出为你收集整理的asp.net mvc中实现Forms身份验证身份验证流程的实例全部内容,希望文章能够帮你解决asp.net mvc中实现Forms身份验证身份验证流程的实例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)