コード例 #1
0
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public string Login(string account, string password)
        {
            account  = account.Trim();
            password = password.Trim();
            List <Claim> claims = new List <Claim>();

            // 如果有登录的委托方法,使用委托方法
            if (OnLogin != null)
            {
                claims = OnLogin(new LoginView {
                    Account = account, Password = password
                });
            }
            else
            {
                // 暂不添加游客的登录处理
            }

            claims.Add(new Claim("type", "AuthVisitorService"));

            var    identity = new ClaimsIdentity(new GenericIdentity(account, "Token"), claims);
            string token    = Jwt.GenerateJwtToken(account, identity, _tokenOptions);

            return(token);
        }
コード例 #2
0
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public string Login(string account, string password)
        {
            account  = account.Trim();
            password = password.Trim();
            //查询密码正确的可用用户列表
            var pwdQuery = _employeeDal.Queryable().Where(e => e.LogonPassword == password && e.IsDelete == false).ToList();

            // 通过账号和密码验证登录
            var employeeList = pwdQuery.Where(e => e.LogonAccount == account);

            if (employeeList.Count() > 1)
            {
                throw ApiException.BadRequest("有多个满足条件的用户,无法登陆。");
            }

            // 如果查找不到用户信息,并且允许用电话登录,尝试电话号码+登录密码登录
            if (employeeList.Count() == 0 && AuthServiceOption.Option.IsLogonByTelephone)
            {
                employeeList = pwdQuery.Where(e => e.Telephone == account || e.Mobile == account);
                if (employeeList.Count() > 1)
                {
                    throw ApiException.BadRequest("有多个满足条件的用户,无法通过电话登陆。");
                }
            }

            var employee = employeeList.FirstOrDefault();

            if (employee == null)
            {
                throw ApiException.BadRequest("您的登陆账号或密码错误。");
            }

            if (employee.State == UserState.Disable)
            {
                throw ApiException.BadRequest("您的登陆功能已被禁用,请与管理员联系。");
            }
            if (employee.State == UserState.LogonLock)
            {
                throw ApiException.BadRequest("多次登陆失败,登陆已被锁住,请与管理员联系。");
            }

            // 执行扩展的登录事件
            OnLogin?.Invoke(employee);

            List <Claim> claims = new List <Claim>
            {
                new Claim("id", employee.Id.ToString()),
                new Claim("name", employee.Name ?? ""),
                new Claim("roleId", employee.Role?.Id ?? ""),
                new Claim("depId", employee.Department?.Id ?? ""),
                new Claim("depName", employee.Department?.Name ?? ""),
                new Claim("type", "AuthService")
            };


            var    identity = new ClaimsIdentity(new GenericIdentity(account, "Token"), claims);
            string token    = Jwt.GenerateJwtToken(account, identity, _tokenOptions);

            return(token);
        }