예제 #1
0
        public async Task <IDataResult <TokenModel> > SignInAsync(SignInModel signInModel)
        {
            var validation = new SignInModelValidator().Validate(signInModel);

            if (validation.IsError)
            {
                return(DataResult <TokenModel> .Error(validation.Message));
            }

            var signedInModel = await _userRepository.SignInAsync(signInModel);

            validation = _signInService.Validate(signedInModel, signInModel);

            if (validation.IsError)
            {
                return(DataResult <TokenModel> .Error(validation.Message));
            }

            var userLogModel = new UserLogModel(signedInModel.Id, LogType.SignIn);

            await _userLogApplicationService.AddAsync(userLogModel);

            await _unitOfWork.SaveChangesAsync();

            var tokenModel = _signInService.CreateToken(signedInModel);

            return(DataResult <TokenModel> .Success(tokenModel));
        }
        public async Task <IDataResult <TokenModel> > SignInAsync(SignInModel signInModel)
        {
            var validation = new SignInModelValidator().Validate(signInModel);

            if (validation.Failed)
            {
                return(DataResult <TokenModel> .Fail(validation.Message));
            }

            var signedInModel = await _userRepository.SignInAsync(signInModel);

            validation = _signInService.Validate(signedInModel, signInModel);

            if (validation.Failed)
            {
                return(DataResult <TokenModel> .Fail(validation.Message));
            }

            var userLogModel = UserLogFactory.Create(signedInModel);

            await _userLogApplicationService.AddAsync(userLogModel);

            var tokenModel = _signInService.CreateToken(signedInModel);

            return(DataResult <TokenModel> .Success(tokenModel));
        }
예제 #3
0
        public async Task <IActionResult> Login([FromBody] CredentialsDto credentials)
        {
            var user = await _userService.FindByNameAsync(credentials.UserNameOrEmail);

            if (user == null)
            {
                user = await _userService.FindByEmailAsync(credentials.UserNameOrEmail);

                if (user == null)
                {
                    return(BadRequest("Login fehlgeschlagen!"));
                }
            }
            user.LastLoginDate = DateTime.Now;
            await _userService.UpdateUserAsync(user);

            await userLoginService.CreateAsync(new UserLogin()
            {
                DateTimeOfLastLogin = DateTime.Now, UserId = user.Id
            });

            if (user.UserLockEnabled)
            {
                return(BadRequest("Benutzer ist gesperrt! Bitte den Administrator kontaktieren"));
            }

            var signInAsync = await _signInService.CheckPasswordSignInAsync(user, credentials.Password);

            if (signInAsync.Succeeded)
            {
                var userDto = _mapper.Map <UserDto>(user);
                var roles   = await _userService.GetRolesAsync(user);

                List <Claim> claims;
                if (!user.IsSuperAdmin)
                {
                    claims = await GetAllClaimsOfUser(roles);
                }
                else
                {
                    claims = RoleClaims.GetAllClaims();
                }
                userDto.AccessToken = _signInService.CreateToken(user, roles, claims);
                return(Ok(userDto));
            }

            return(BadRequest("Login fehlgeschlagen!"));
        }