Exemplo n.º 1
0
        /// <summary>
        /// 本地登录逻辑
        /// </summary>
        /// <param name="userInfo"></param>
        /// <param name="password"></param>
        /// <param name="msg"></param>
        /// <param name="failCount"></param>
        /// <returns></returns>
        private bool DoLogin(UserDetail userInfo, string password, ref string msg)
        {
            msg = CheckUserInfo(userInfo, password);
            if (!string.IsNullOrWhiteSpace(msg))
            {
                return(false);
            }

            // token 通过 credentials.accesstoken 返回
            CredentialsExt credentials = MakeToken(userInfo, userIp, string.Empty, "pc", DateTime.Now.AddDays(1), Constants.USER_TYPE_Cloud);

            if (credentials != null)
            {
                //通过token踢掉所有同用户名的其他在线用户
                _credentialsRepository.KickCredentialsByToken(credentials.accesstoken);

                ClearSession();
                ClearCookies();

                // 获取角色
                credentials.Roles = GetRoles(credentials);

                string userData = JsonConvert.SerializeObject(credentials);

                DateTime expiration = DateTime.Now.Add(FormsAuthentication.Timeout);
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, credentials.UserName, DateTime.Now, expiration, true,
                                                                                 userData, FormsAuthentication.FormsCookiePath);

                //IPrincipal principal = HttpContext.Current.User;
                //principal.Identity =
                //FormsIdentity formsIdentity = new FormsIdentity(ticket);
                //HttpContext.Current.User.Identity = formsIdentity;

                HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket))
                {
                    HttpOnly = true,
                    Expires  = expiration
                };

                HttpContext.Current.Session[Constants.IDENDITY_SESSION_NAME] = credentials;
                HttpContext.Current.Response.Cookies.Remove(cookie.Name);
                HttpContext.Current.Response.Cookies.Add(cookie);

                return(true);
            }
            else
            {
                return(false);
            }
        }