public Result SignIn(Login login) { Result ret = new Result(); try { string pwd = EncryptionMD5.EncryptString(login.password); if (LoadEntities(l => l.Name == login.username && l.Password == pwd).Any()) { SysPerson sp = LoadEntities(l => l.Name == login.username && l.Password == pwd).FirstOrDefault(); string UserData = login.username + "#" + login.password + "#" + sp.MyName; //数据放入ticket FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, login.username, DateTime.Now, DateTime.Now.AddSeconds(30), false, UserData); //数据加密 string enyTicket = FormsAuthentication.Encrypt(ticket); CookieHelper.SetCookie(FormsAuthentication.FormsCookieName, enyTicket); ret.Msg = "登录成功!"; // } else { ret.Msg = "用户名或密码错误!"; } } catch (Exception) { ret.Msg = "登录失败!"; throw; } return(ret); }
/// <summary> /// 修改用户 /// </summary> /// <param name="users"></param> /// <returns></returns> public Result EdtUser(TB_Users users) { Result result = new Result(); if (users.user_id == 0) { result.Code = "400"; result.Msg = "要修改的ID不能为空!"; } else { if (LoadEntities(s => s.user_id == users.user_id).Any()) { users.user_password = EncryptionMD5.EncryptString(users.user_password); users.modifytime = DateTime.Now; UpdateEntity(users); result.Code = "200"; result.Msg = "修改成功!"; } else { result.Code = "400"; result.Msg = "该用户不存在!"; } } return(result); }
/// <summary> /// 登录 /// </summary> /// <returns></returns> public Result Login(string LoginName, string PassWord) { Result r = new Result(); PassWord = EncryptionMD5.EncryptString(PassWord); if (!ValidateUser(LoginName, PassWord)) { r.Code = "404"; r.Msg = "用户名或密码错误"; return(r); } FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(0, LoginName, DateTime.Now, DateTime.Now.AddHours(1), true, string.Format("{0}&{1}", LoginName, PassWord), FormsAuthentication.FormsCookiePath); string fullname = LoadEntities(s => s.user_name == LoginName).Select(s => s.fullname).SingleOrDefault(); //返回登录结果、用户信息、用户验证票据信息 var Users = new TB_Users { user_name = LoginName, user_password = PassWord, fullname = fullname, Ticket = FormsAuthentication.Encrypt(ticket) }; //将身份信息保存在session中,验证当前请求是否是有效请求 // CookieHelper.SetCookie(LoginName, PassWord, DateTime.Now.AddMinutes(20)); CookieHelper.SetCookie(LoginName, PassWord); CookieHelper.SetCookie(LoginName + "fullname", fullname); r.Code = "200"; r.Msg = "登录成功!"; r.Data = Users; return(r); }
/// <summary> /// 添加用户 /// </summary> /// <param name="users"></param> /// <returns></returns> public Result AddUser(TB_Users users) { Result result = new Result(); if (LoadEntities(s => s.user_name == users.user_name).Any()) { result.Code = "400"; result.Msg = "该名称已存在!"; } else { users.user_password = EncryptionMD5.EncryptString(users.user_password); users.status = "1"; users.createtime = DateTime.Now; result.Data = AddEntity(users); result.Code = "200"; result.Msg = "添加成功!"; } return(result); }