コード例 #1
0
        public async Task <LoginUserResponse> LoginAsync(string email, string username, string password, string remoteIpAddress)
        {
            if (string.IsNullOrEmpty(email) && string.IsNullOrEmpty(username))
            {
                return(new LoginUserResponse(
                           new List <Error> {
                    new Error("login_failure", "Username or email needs to be provided")
                },
                           false));
            }

            AppUser user = null;

            if (!string.IsNullOrEmpty(username))
            {
                user = await this.userManager.FindByNameAsync(username);
            }
            else if (!string.IsNullOrEmpty(email))
            {
                user = await this.userManager.FindByEmailAsync(email);
            }

            if (user == null || !(await this.userManager.CheckPasswordAsync(user, password)))
            {
                return(new LoginUserResponse(
                           new List <Error> {
                    new Error("login_failure", "Invalid username/email or password")
                },
                           false));
            }

            var tokens = this.dbContext.Users.Where(x => x.Id == user.Id).Include(x => x.RefreshTokens).ToList();

            var refreshToken = this.tokenFactory.GenerateToken();

            user.AddRefreshToken(refreshToken, user.Id, remoteIpAddress);


            await this.userManager.UpdateAsync(user);

            return(new LoginUserResponse(await this.jwtTokenFactory.GenerateEncodedTokenAsync(user.Id, user.UserName), refreshToken, true));
        }