Example #1
0
File: Main.cs Project: jzh225/NetC
        /// <summary>
        /// 用户登录
        /// </summary>
        public static string Login(string loginName, string plaintext, string ipAddress, string checkCode, bool IsAuto = false)
        {
            if (!VerifyCode.Validate(checkCode))
            {
                return("验证码输入错误");
            }

            var model = AdminService.Login(loginName, plaintext, ipAddress);

            //判断是否登录成功
            if (model != null)
            {
                #region 保存票据
                UserLoginEncrypt userLoginEncryptData = new UserLoginEncrypt();
                userLoginEncryptData.UserData = string.Format(Config.userData
                                                              , model.Id
                                                              , model.UserName
                                                              , plaintext
                                                              , model.Name
                                                              , model.Enabled
                                                              , model.LastLoginTime
                                                              , model.LastLoginIp
                                                              , model.IsAdmin
                                                              );

                if (!string.IsNullOrEmpty(userLoginEncryptData.UserData))
                {
                    //写入Session值
                    if (!string.IsNullOrEmpty(userLoginEncryptData.UserData))
                    {
                        UserLoginData _data = XmlToModel.ToUser(userLoginEncryptData.UserData);
                        if (_data != null)
                        {
                            SessionUser.WriteSession(_data.Id);
                            SessionUser.WriteSession(_data);
                        }
                        //用于校验cookie值是否被修改和Session的及时性
                        SessionUser.WriteSessionMd5(Security.Md5(userLoginEncryptData.UserData));
                    }
                    //写入Cookie
                    userLoginEncryptData.UserData = Security.DesEncrypt(userLoginEncryptData.UserData);
                    if (IsAuto)
                    {
                        Cookie.WriteCookie(userLoginEncryptData, DateTime.Now.AddDays(7));
                    }
                    else
                    {
                        Cookie.WriteCookie(userLoginEncryptData, DateTime.Now.AddHours(1));
                    }

                    #region 单用户登录
                    HttpContext.Current.Session[Config.sessionUserGUID] = Guid.NewGuid().ToString("N");
                    CookieUtility.Save(Config.cookiesUserGUID, HttpContext.Current.Session[Config.sessionUserGUID].ToString(), 10);
                    Hashtable hOnline = (Hashtable)HttpContext.Current.Application["Online"];
                    if (hOnline != null)
                    {
                        IDictionaryEnumerator idE = hOnline.GetEnumerator();
                        string strKey             = "";
                        while (idE.MoveNext())
                        {
                            if (idE.Value != null && idE.Value.ToString().Equals(model.Id.ToString()))
                            {
                                strKey          = idE.Key.ToString();
                                hOnline[strKey] = "XXXXXX";
                                break;
                            }
                        }
                    }
                    else
                    {
                        hOnline = new Hashtable();
                    }
                    hOnline[HttpContext.Current.Session[Config.sessionUserGUID].ToString()] = model.Id;
                    HttpContext.Current.Application.Lock();
                    HttpContext.Current.Application["Online"] = hOnline;
                    HttpContext.Current.Application.UnLock();
                    #endregion
                }
                #endregion
                return("");
            }
            else
            {
                return("登录名或密码错误");
            }
        }