/// <summary> /// 退出 /// </summary> /// <returns></returns> public ActionResult LogOut() { try { MyFormsAuthentication.SignOut(); return(Redirect("~/Account/Login")); } catch (Exception ex) { LogHelper.WriteLog(ex.Message); throw ex; } }
public ActionResult Register(BBSUserVM user) { if (user == null || string.IsNullOrEmpty(user.LoginName) || string.IsNullOrEmpty(user.Password)) { return(Json(new { Code = -400, Msg = "参数不能为空", Data = "" })); } try { BBSUserService userSV = new BBSUserService(); if (userSV.IsLoginNameExist(user.LoginName)) { return(Json(new { Code = -200, Msg = "用户名已存在", Data = "" })); } user.InputTime = DateTime.Now; user = userSV.Register(user); MyFormsAuthentication.SetAuthCookie(user.LoginName, new MyFormsAuthentication() { UserID = user.ID, UserName = user.LoginName }, false); return(Json(new { Code = 200, Msg = "注册成功", Data = user })); } catch (Exception ex) { LogHelper.WriteLog(ex.Message); throw ex; } }
public static bool Login(string username, string password) { bool IsFlg = false; MyUserDataPrincipal userData = null; if (username == "admin" && password == "123456") { userData = new MyUserDataPrincipal(); userData.UserId = Guid.Empty; userData.UserModel.Add("UserName", "admin"); userData.UserModel.Add("TelePhone", "13026209315"); userData.UserModel.Add("DepId", Guid.Empty); userData.UserModel.Add("DepName", "LCL"); IsFlg = true; } else { var repo = RF.Concrete <IUserRepository>(); var user = repo.GetBy(username, password); if (user != null) { var rolesIds = repo.GetRolesIds(user.ID); var groupIds = repo.GetGroupIds(user.ID); userData = new MyUserDataPrincipal(); userData.UserId = user.ID; userData.RoleId = rolesIds; userData.GroupId = groupIds; userData.UserModel.Add("UserName", user.Name); userData.UserModel.Add("TelePhone", user.Telephone); userData.UserModel.Add("DepId", user.Department == null ? Guid.Empty : user.Department.ID); userData.UserModel.Add("DepName", user.Department == null ? "" : user.Department.Name); IsFlg = true; } } if (IsFlg) { MyFormsAuthentication <MyUserDataPrincipal> .SetAuthCookie(username, userData, false); //保存Cookie } return(IsFlg); }
public IHttpActionResult SignIn() { MyFormsAuthenticationTicket ticket = new MyFormsAuthenticationTicket( 2, new Random().Next().ToString(), DateTime.Now, DateTime.Now.AddDays(1), true, "admin,张三"); string encryptStr = MyFormsAuthentication.EncryptDES(JsonConvert.SerializeObject(ticket), "11111111"); HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptStr); //cookie.HttpOnly = true; cookie.Secure = FormsAuthentication.RequireSSL; HttpContext context = HttpContext.Current; if (context == null) { throw new InvalidOperationException(); } // 写入Cookie context.Response.Cookies.Remove(cookie.Name); context.Response.Cookies.Add(cookie); return(Ok("登陆成功")); }
protected void Application_PostAuthenticateRequest(object sender, System.EventArgs e) { var formsIdentity = HttpContext.Current.User.Identity as FormsIdentity; if (formsIdentity != null && formsIdentity.IsAuthenticated && formsIdentity.AuthenticationType == "Forms") { var exts = new[] { ".js", ".css", ".gif", ".png", ".jpg", ".swf", ".xls", ".csv", ".xlsx", ".txt" }; if (exts.Contains(HttpContext.Current.Request.CurrentExecutionFilePathExtension)) { return; } var principal = MyFormsAuthentication <MyUserDataPrincipal> .TryParsePrincipal(HttpContext.Current.Request); if (null != principal && principal.UserState.UserState.UserFuncs == null) { principal.UserState.UserState.UserFuncs = GetUserFuncsCache(principal.UserState.UserState.UserID); principal.UserState.UserState.UserOrgs = GetUserOrgsCache(principal.UserState.UserState.UserID); } HttpContext.Current.User = principal; } }
public ActionResult Login(UserVM user) { if (user == null) { return(Json(new { Code = -400, Msg = "参数不能为空", Data = "" })); } if (string.IsNullOrEmpty(user.LoginName) || string.IsNullOrEmpty(user.Password)) { return(Json(new { Code = -400, Msg = "用户名或密码不能为空", Data = "" })); } UserService userSV = new UserService(); user.InputTime = DateTime.Now; userSV.Login(user); if (user == null || !user.ID.HasValue) { return(Json(new { Code = -200, Msg = "用户不存在", Data = "" })); } #region 添加登录cookie //FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, user.LoginName, DateTime.Now, DateTime.Now.AddDays(1), false, JsonConvert.SerializeObject(user)); //FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(user.LoginName, false, 30); //string encryptTicket = FormsAuthentication.Encrypt(ticket); //HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptTicket) //{ // Expires = DateTime.Now.AddMinutes(5) //}; //System.Web.HttpContext.Current.Response.Cookies.Add(authCookie); MyFormsAuthentication.SetAuthCookie(user.LoginName, new MyFormsAuthentication() { UserID = user.ID, UserName = user.LoginName }, false); #endregion return(Json(new { Code = 200, Msg = "登录成功", Data = user })); }
/// <summary> /// 退出 /// </summary> /// <returns></returns> public ActionResult Logout() { MyFormsAuthentication.RemoveAuthCookie(); return(RedirectToAction("Login", "Account")); }
public ActionResult Login(LoginModel model, string returnUrl) { try { #region 数据验证 if (string.IsNullOrEmpty(model.UserID)) { Alter("用户名不能为空。", util.Enum.AlterTypeEnum.Warning); return(View()); } if (string.IsNullOrEmpty(model.Password)) { Alter("密码不能为空。", util.Enum.AlterTypeEnum.Warning); return(View()); } if (string.IsNullOrEmpty(model.Code)) { Alter("请输入验证码。", util.Enum.AlterTypeEnum.Warning); return(View()); } if (Check(model.Code) == false) { Alter("验证码输入错误。", util.Enum.AlterTypeEnum.Warning); return(View()); } #endregion string pwd = CryptTools.Md5(model.Password); var user = dao.GetEntity("USER_ID", model.UserID);//, "USER_PASSWORD", pwd if (user == null) { AddLoginLog(SYS_LOGINLOG.STATE_ENUM.用户不存在, model.UserID); Alter("用户不存在!", util.Enum.AlterTypeEnum.Error); ModelState.AddModelError("", "用户不存在!"); return(View()); } if (user.USER_PASSWORD != pwd) { AddLoginLog(SYS_LOGINLOG.STATE_ENUM.密码错误, model.UserID); Alter("密码错误!", util.Enum.AlterTypeEnum.Error); ModelState.AddModelError("", "密码错误!"); return(View()); } var us = new UserState(); us.UserID = user.USER_ID; us.UserName = user.USER_NAM; #region //验证成功 var userData = new MyUserDataPrincipal { UserState = us }; MyFormsAuthentication <MyUserDataPrincipal> .SetAuthCookie(us.UserName, userData, model.RememberMe); GetUserFuncsCache(us.UserID); #endregion AddLoginLog(SYS_LOGINLOG.STATE_ENUM.登录成功, model.UserID); return(Redirect("/")); } catch (Exception e) { LogHelper.ErrorLog("登录出错。", e); ModelState.AddModelError("", "登录出错。"); return(View()); } }