Ejemplo n.º 1
0
        /// <inheritdoc />
        public async Task <IUserActionResult> Create(T user, string password)
        {
            var identityResult = await _userManager.CreateAsync(user, password);

            if (identityResult.Errors.Any())
            {
                return(new DefaultUserActionResultBuilder()
                       .WithIdentityErrors(identityResult.Errors)
                       .Build());
            }

            //Generate tokens for the user
            var jwt          = _jwtHandler.Generate(DefaultJwtHandler.GetDefaultClaims(user.FirstName, user.Email));
            var refreshToken = _refreshTokenGenerator.Generate();

            //Add token to db and update user
            user.RefreshTokens.Add(refreshToken);
            await _userManager.UpdateAsync(user);

            return(new DefaultUserActionResultBuilder()
                   .Success()
                   .WithJwt(jwt)
                   .WithRefreshToken(refreshToken.Token)
                   .Build());
        }
Ejemplo n.º 2
0
        public async Task <string> LoginAsync(AuthenticateUser model)
        {
            var user = await _applicationUserRepository.GetAsync(model.UserName);

            if (user == null)
            {
                throw new TcsException("invalid_credentials",
                                       "User cannot be find.");
            }

            var result = await _signInManager.CheckPasswordSignInAsync(user
                                                                       , model.Password, true);

            if (!result.Succeeded)
            {
                throw new TcsException("invalid_credentials",
                                       "invalid_credentials.");
            }

            return(_jwtHandler.Generate(user));
        }
Ejemplo n.º 3
0
        public async Task <DataResult <string> > LoginUserAsync(LoginDto model)
        {
            var user = await _userManager.Users.SingleOrDefaultAsync(u => u.UserName == model.UserName);

            if (user == null)
            {
                throw new EducaException("invalid_credentials",
                                         "User cannot be find.");
            }

            var result = await _signInManager.CheckPasswordSignInAsync(user
                                                                       , model.Password, true);

            if (!result.Succeeded)
            {
                throw new EducaException("invalid_credentials",
                                         "invalid credentials.");
            }

            var roles = await _userManager.GetRolesAsync(user);

            return(new DataResult <string>(_jwtHandler.Generate(user, roles.ToList())));
        }