/// <summary> /// 0成功 1用户不存在 2密码错误 3 验证码错误 4账号已冻结 /// </summary> /// <param name="context"></param> /// <param name="name"></param> /// <param name="pwd"></param> /// <param name="verify"></param> /// <returns></returns> public static LoginResult UserLogin(this HttpContextBase context, string name = "", string pwd = "", string verify = "") { if (string.IsNullOrEmpty(verify) || context.Session["CheckCode"] == null || !context.Session["CheckCode"].ToString().Equals(verify, StringComparison.OrdinalIgnoreCase)) { return(LoginResult.WrongVerify); } IUserMenuService service = DIFactory.GetContainer().Resolve <IUserMenuService>(); User user = service.UserLogin(name); if (user == null) { return(LoginResult.NoUser); } else if (!user.Password.Equals(MD5Encrypt.Encrypt(pwd))) { return(LoginResult.WrongPwd); } else if (user.State == (int)UserState.Frozen) { return(LoginResult.Frozen); } else { #region Cookie CurrentUser currentUser = new CurrentUser() { Id = user.Id, Name = user.Name, Account = user.Account, Email = user.Email, Password = user.Password, LoginTime = DateTime.Now }; //HttpCookie cookie = context.Request.Cookies.Get("CurrentUser"); //if (cookie == null) //{ HttpCookie myCookie = new HttpCookie("CurrentUser"); myCookie.Value = JsonHelper.ObjectToString <CurrentUser>(currentUser); myCookie.Expires = DateTime.Now.AddMinutes(5); context.Response.Cookies.Add(myCookie); //} #endregion Cookie #region Session //context.Session.RemoveAll(); var sessionUser = context.Session["CurrentUser"]; context.Session["CurrentUser"] = currentUser; context.Session.Timeout = 3;//minute session过期等于Abandon #endregion Session logger.Debug(string.Format("用户id={0} Name={1}登录系统", currentUser.Id, currentUser.Name)); service.LastLogin(user); return(LoginResult.Success); } }
/// <summary> /// 0成功 1用户不存在 2密码错误 3 验证码错误 4账号已冻结 /// </summary> /// <param name="context"></param> /// <param name="name"></param> /// <param name="pwd"></param> /// <param name="verify"></param> /// <returns></returns> public static LoginResult UserLogin(this HttpContextBase context, string name = "", string pwd = "", string verify = "") { if (string.IsNullOrEmpty(verify) || context.Session["CheckCode"] == null || !context.Session["CheckCode"].ToString().Equals(verify, StringComparison.OrdinalIgnoreCase)) { return(LoginResult.WrongVerify); } IUserMenuService service = DIFactory.GetContainer().Resolve <IUserMenuService>(); User user = service.UserLogin(name); if (user == null) { return(LoginResult.NoUser); } else if (!user.Password.Equals(MD5Encrypt.Encrypt(pwd))) { return(LoginResult.WrongPwd); } else if (user.State == (int)UserState.Frozen) { return(LoginResult.Frozen); } else { //Response,Request,Application,Server,Session #region Server //辅助类 Server string encode = context.Server.HtmlEncode("<我爱我家>"); string decode = context.Server.HtmlDecode(encode); string physicalPath = context.Server.MapPath("/home/index");//只能做物理文件的映射 string encodeUrl = context.Server.UrlEncode("<我爱我家>"); string decodeUrl = context.Server.UrlDecode(encodeUrl); #endregion #region Application context.Application.Lock();//ASP.NET 应用程序内的多个会话和请求之间共享信息 context.Application.Lock(); context.Application.Add("try", "die"); context.Application.UnLock(); object aValue = context.Application.Get("try"); aValue = context.Application["try"]; context.Application.Remove("命名对象"); context.Application.RemoveAt(0); context.Application.RemoveAll(); context.Application.Clear(); context.Items["123"] = "123";//单一会话,不同环境都可以用 #endregion #region Cookie CurrentUser currentUser = new CurrentUser() { Id = user.Id, Name = user.Name, Account = user.Account, Email = user.Email, Password = user.Password, LoginTime = DateTime.Now }; //HttpCookie cookie = context.Request.Cookies.Get("CurrentUser"); //if (cookie == null) //{ HttpCookie myCookie = new HttpCookie("CurrentUser"); myCookie.Value = JsonHelper.ObjectToString <CurrentUser>(currentUser); myCookie.Expires = DateTime.Now.AddMinutes(5); context.Response.Cookies.Add(myCookie); //} #endregion Cookie #region Session //context.Session.RemoveAll(); var sessionUser = context.Session["CurrentUser"]; context.Session["CurrentUser"] = currentUser; context.Session.Timeout = 3;//minute session过期等于Abandon #endregion Session logger.Debug(string.Format("用户id={0} Name={1}登录系统", currentUser.Id, currentUser.Name)); service.LastLogin(user); return(LoginResult.Success); } }