/// <summary> /// 得到Session的字符串类型 /// </summary> /// <param name="model"></param> /// <returns></returns> public static string GetStr(SessionInfo model) { string str; string symbol = @"$$"; str = "UserID=" + model.UserID + symbol + "UserName="******"AdminType=" + model.AdminType + symbol + "FactionID=" + model.FactionID + symbol + "FactionDeID=" + model.FactionDeID + symbol + "ShowSelfQM=" + model.ShowSelfQM + symbol + "ShowAllQM=" + model.ShowAllQM + symbol + "ShowPopup=" + model.ShowPopup + symbol + "LastCheckTime=" + model.LastCheckTime + symbol + "LastPostTime=" + model.LastPostTime + symbol + "GamePassword="******"MaxBBSNoticeID=" + model.MaxBBSNoticeID+ symbol; //if(model.ShowSelfQM==true) // str+="ShowSelfQM=1"+symbol; //else // str+="ShowSelfQM=0"+symbol; //if(model.ShowAllQM==true) // str+="ShowAllQM=1"+symbol; //else // str+="ShowAllQM=0"+symbol; //if(model.ShowPopup==true) // str+="ShowPopup=1"+symbol; //else // str+="ShowPopup=0"+symbol; return str; }
/// <summary> /// 得到用户的实体 /// </summary> /// <param name="modelStr"></param> /// <returns></returns> public static SessionInfo GetModel(string modelStr) { //string[] arrUser = System.Text.RegularExpressions.Regex.Split(modelStr, @"\$\$"); SessionInfo model = new SessionInfo(); model.UserID = int.Parse(GetConfigValue(modelStr,SessionName.UserID)); model.UserName = GetConfigValue(modelStr, SessionName.UserName); model.AdminType = int.Parse(GetConfigValue(modelStr, SessionName.AdminType)); model.FactionID = int.Parse(GetConfigValue(modelStr, SessionName.FactionID)); model.FactionDeID = int.Parse(GetConfigValue(modelStr, SessionName.FactionDeID)); model.ShowSelfQM = bool.Parse(GetConfigValue(modelStr, SessionName.ShowSelfQM)); model.ShowAllQM = bool.Parse(GetConfigValue(modelStr, SessionName.ShowAllQM)); model.ShowPopup = bool.Parse(GetConfigValue(modelStr, SessionName.ShowPopup)); //model.ShowSelfQM = GetConfigValue(modelStr, SessionName.ShowSelfQM) == "0" ? false : true; //model.ShowAllQM = GetConfigValue(modelStr, SessionName.ShowAllQM) == "0" ? false : true; //model.ShowPopup = GetConfigValue(modelStr, SessionName.ShowPopup) == "0" ? false : true; model.LastCheckTime = DateTime.Parse(GetConfigValue(modelStr, SessionName.LastCheckTime)); model.LastPostTime = DateTime.Parse(GetConfigValue(modelStr, SessionName.LastPostTime)); model.GamePassword = GetConfigValue(modelStr, SessionName.GamePassword); try { model.MaxBBSNoticeID = int.Parse(GetConfigValue(modelStr, SessionName.MaxBBSNoticeID)); } catch { model.MaxBBSNoticeID = 0; } return model; }
public ActionResult LogOn(int UserId, string UserName, string UserConfig) { SessionInfo model = new SessionInfo(); model.UserID = UserId; model.UserName = UserName; model.AdminType = 8; SessionOper.SessionSet(HttpContext, UserId, model, true); return RedirectToAction("Index", "Home"); }
/// <summary> /// 基于Forms验证,得到会话用户信息 /// </summary> /// <param name="context">HttpContext上下文</param> /// <param name="UserID">用户ID</param> /// <param name="UserData">用户自定义数据</param> /// <returns></returns> public static bool SessionGet(HttpContext context, out int UserID, out SessionInfo UserData) { if (context.User.Identity.IsAuthenticated) { FormsIdentity Id = (FormsIdentity)context.User.Identity; FormsAuthenticationTicket Ticket = Id.Ticket; //取得身份验证票 UserID = int.Parse(Ticket.Name); UserData = SessionInfoOper.GetModel(Ticket.UserData); return true; } UserID = 0; UserData = null; return false; }
/// <summary> /// 基于Forms验证,设置Session会话 /// </summary> /// <param name="context">HttpContext上下文</param> /// <param name="UserID">用户ID</param> /// <param name="UserData">用户自定义数据</param> /// <param name="IsPersistent">cookies是否持久化</param> public static void SessionSet(HttpContextBase context, int UserID, SessionInfo UserData, bool IsPersistent) { FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(1, UserID.ToString(), DateTime.Now, DateTime.Now.AddYears(1), IsPersistent, SessionInfoOper.GetStr(UserData), FormsAuthentication.FormsCookiePath); string HashTicket = FormsAuthentication.Encrypt(Ticket); //把角色信息保存到Cookie中去 HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, HashTicket) { HttpOnly = true, Path = Ticket.CookiePath, Expires = Ticket.IsPersistent ? Ticket.Expiration : DateTime.MinValue, Domain = FormsAuthentication.CookieDomain, }; context.Response.Cookies.Add(UserCookie); }
/// <summary> /// 统一登录处理 /// </summary> /// <param name="userName"></param> /// <param name="password">SHA1加密后的密码</param> /// <returns></returns> public static string LoginDeal(HttpContextBase context, string userName, string password) { if (userName == "") return "用户名不能为空"; if (password == "") return "密码不能为空"; string ip = Public.CommonHelper.GetIPAddress(); bizUsers b = new bizUsers(); bizUserPwdLog bul = new bizUserPwdLog(); int testLoginNum = bul.GetNum(ip); if (testLoginNum >= 5) { return "请稍后再试"; } Users model = b.Get(userName); if (model == null || MyText.GetEncrypt(model.Password) != password) { bul.Add(MyText.SafeStr(userName), password, ip); return "用户名或密码错误,你还有" + (5 - testLoginNum) + "次机会"; } if ((new bizForbidIP()).Exist(ip)) { return "你的IP已被封"; } //设置Session SessionInfo modelSession = new SessionInfo(); modelSession.UserID = model.UserID; modelSession.UserName = model.UserName; modelSession.AdminType = model.AdminType; modelSession.ShowAllQM = UserCommon.ShowAllQM(model.UserConfig); modelSession.ShowSelfQM = UserCommon.ShowSelfQM(model.UserConfig); modelSession.ShowPopup = UserCommon.ShowPopup(model.UserConfig); modelSession.MaxBBSNoticeID = MyCache.BBSCache.maxBBSNoticeID; modelSession.FactionID = model.FactionID; modelSession.FactionDeID = model.FactionDeID; SessionOper.SessionSet(context, model.UserID, modelSession, true); b.LoginDeal(model.UserID, ip); SZXX.Common.Untilies.OnlineUsers.Instance.AddUser(model.UserID, model.UserName, model.UserColor, model.AdminType); //设置Cookie HttpCookie myc = new HttpCookie("MyInfo"); myc["Name"] = HttpUtility.UrlEncode(model.UserName); myc["Code"] = MyText.GetEncrypt(model.Password); myc.Expires = DateTime.Now.AddYears(1); myc.Domain = ConfigurationManager.AppSettings["RootDomain"]; HttpContext.Current.Response.AppendCookie(myc); return ""; }
/// <summary> /// ����Session /// </summary> /// <param name="model"></param> public void SessionSet(SessionInfo model) { SessionOper.SessionSet(HttpContext, model.UserID, model, true); }