Beispiel #1
0
        public async Task <Result <AccessToken> > Login([FromBody] AuthoriseByUsernameRequestDto dto)
        {
            //验证请求数据合法性
            var result = dto.Valid();

            if (!result.Success)
            {
                HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                return(Result <AccessToken> .ReFailure(result));
            }

            //账户登录验证
            var service = this._serviceProvider.GetRequiredService <AccountLoginService>();
            var ip      = HttpContext.Connection.RemoteIpAddress?.ToString();

            if (string.IsNullOrEmpty(ip))
            {
                ip = "127.0.0.1";
            }
            var logResult = service.LoginByUsername(dto.Username, dto.Password, ip);

            if (!logResult.Success)
            {
                HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                return(Result <AccessToken> .ReFailure(logResult));
            }

            //数据持久化
            await _accountRepository.ModifyAsync(logResult.Data);

            await _unitOfWork.CommitAsync(autoRollback : true);

            return(Result <AccessToken> .ReSuccess(logResult.Data.LoginInfo.AccessToken));
        }
Beispiel #2
0
        public async Task <Result <AccessToken> > Login([FromBody] AuthoriseByUsernameRequestDto dto)
        {
            //账户登录验证
            var service = this._serviceProvider.GetRequiredService <IAccountLoginService>();
            var ip      = HttpContext.Connection.RemoteIpAddress?.ToString();

            if (string.IsNullOrEmpty(ip))
            {
                ip = "127.0.0.1";
            }
            if (ip.Equals("::1"))
            {
                ip = "127.0.0.1";
            }
            // 如果为手机号码就试用手机号码/密码登录
            if (Regex.IsMatch(dto.Username, @"^1[0-9]{10}$") && dto.LoginType.Equals(LoginTypeValue.Mobile.ToString().ToLower()))
            {
                return(await service.LoginByTelephone(new PhoneNumber(dto.Username, 86), dto.TenancyId, dto.Password, ip));
            }
            // 如果为有限就使用邮箱/密码登录
            if (Regex.IsMatch(dto.Username, @"^[-\w\+]+(?:\.[-\w]+)*@[-a-z0-9]+(?:\.[a-z0-9]+)*(?:\.[a-z]{2,})$") && dto.LoginType.Equals(LoginTypeValue.Email.ToString().ToLower()))
            {
                return(await service.LoginByEmail(dto.Username, dto.TenancyId, dto.Password, ip));
            }
            if (dto.LoginType.Equals(LoginTypeValue.Account.ToString().ToLower()))
            {
                // 否则使用账户密码
                return(await service.LoginByUsername(dto.Username, dto.TenancyId, dto.Password, ip));
            }
            return(Result <AccessToken> .ReFailure(ResultCodes.AccountExistTypeError));
        }