예제 #1
0
        /// <summary>
        /// 尝试登录(会先注销当前已用户)
        /// </summary>
        /// <param name="login_name">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        internal static bool TrySignIn(string login_name, string password, bool autoLogin = false)
        {
            iHealthEntities db = new iHealthEntities();
            //先通过调用数据库存储过程来判断用户密码是否正确
            var result  = new System.Data.Entity.Core.Objects.ObjectParameter("iscorrect", typeof(string));
            var user_id = new System.Data.Entity.Core.Objects.ObjectParameter("user_id", typeof(int));

            db.VeryfyPassword(login_name, password, result, user_id);
            if (result.Value.ToString().Equals("T"))//密码正确
            {
                //注销之前用户
                SignOut();
                //获取用户详细信息
                var user = db.USERINFO.Find((int)user_id.Value);
                if (user == null)
                {
                    return(false);
                }
                //如果启用自动登录,则将包含登录信息的Cookie存入客户端,
                //以便用户下次访问时通过读取Cookie来自动调用此方法登入系统
                if (autoLogin)
                {
                    //创建一个FormsAuthenticationTicket,它包含登录名以及额外的用户数据。
                    var data   = new TicketUserData(user.LOGIN_NM, user.PASSWORD);
                    var ticket = new FormsAuthenticationTicket
                                     (1, user.USER_ID.ToString(), DateTime.Now,
                                     DateTime.Now.AddDays(7), true, data.ToString());
                    //加密Ticket,变成一个加密的字符串。
                    var cookieValue = FormsAuthentication.Encrypt(ticket);
                    //根据加密结果创建登录Cookie
                    var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, cookieValue)
                    {
                        HttpOnly = true,
                        Secure   = FormsAuthentication.RequireSSL,
                        Domain   = FormsAuthentication.CookieDomain,
                        Path     = FormsAuthentication.FormsCookiePath
                    };
                    cookie.Expires = DateTime.Now.AddDays(7);
                    //写登录Cookie
                    HttpContext.Current.Response.Cookies.Remove(cookie.Name);
                    HttpContext.Current.Response.Cookies.Add(cookie);
                }
                // 设置Session值
                HttpContext.Current.Session[loginID]     = user.USER_ID;
                HttpContext.Current.Session[currentUser] = user;
                HttpContext.Current.Session[loginIP]     = CurrentUserIPAddress;
                switch ((GroupType)user.GROUP_ID)
                {
                case GroupType.Patient:
                    HttpContext.Current.Session[personInfoPage] = "Patient";
                    break;

                case GroupType.Doctor:
                    HttpContext.Current.Session[personInfoPage] = "Doctor";
                    break;

                case GroupType.SupeAdmin:
                case GroupType.WebAccendant:
                case GroupType.CodeAccendant:
                case GroupType.DataAccendant:
                default:
                    HttpContext.Current.Session[personInfoPage] = "Home";
                    break;
                }
                return(true);
            }
            return(false);
        }