public async Task <bool> VerifyUserAsync(LoginModelApi model)
        {
            var accountAccountInfo = await GetRawByEmailAsync(model.Email);

            if (accountAccountInfo == null)
            {
                return(false);
            }

            bool res = Crypto.VerifyHashedPassword(accountAccountInfo.Item1.PasswordHash,
                                                   model.Password + accountAccountInfo.Item1.Salt);

            if (res)
            {
                return(true);
            }

            return(false);
        }
Exemplo n.º 2
0
        public async Task Login([FromForm] LoginModelApi loginModel)
        {
            await _apiHealper.DoStandartSomething(
                async() =>
            {
                //throw new Exception("test exc");

                //throw new System.Exception();
                if (_apiHealper.ErrorsFromModelState(ModelState))
                {
                    await _apiHealper.WriteResponseAsync(Response, _errRetrunFactory.GetObjectReturn((_errorService.GetErrorsObject())));
                    return;
                }

                var tokens = await _authSrvice.Login(loginModel.GetModel());
                if (tokens == null)
                {
                    throw new SomeCustomException(ErrorConsts.SomeError);
                }

                _apiHealper.SetUserTokens(Response, tokens);
                await _apiHealper.WriteResponseAsync(Response, _tokensReturnFactory.GetObjectReturn(tokens));
            }, Response, _logger);
        }
        public async Task <IActionResult> Authorize([FromBody] LoginModelApi model)
        {
            var res = await _authJWTService.GetTokenResponseAsync(model);

            return(Ok(res));
        }
        public async Task <ResponseModel <AuthTokenResponseModel> > GetTokenResponseAsync(LoginModelApi loginApiModel)
        {
            var accountApiModel = await accountService.GetByEmailAsync(loginApiModel.Email);

            var respModel = new ResponseModel <AuthTokenResponseModel>();

            if (accountApiModel == null || !await accountService.VerifyUserAsync(loginApiModel))
            {
                respModel.AddError(new Error($"Email or password is incorrect..."));
                return(respModel);
            }

            var claims = GenerateClaims(accountApiModel);

            var jwt = new JwtSecurityToken(
                issuer: this.authJWToptions.Value.Issuer,
                audience: this.authJWToptions.Value.Audience,
                notBefore: DateTime.UtcNow,
                claims: claims.Claims,
                expires: DateTime.UtcNow.Add(TimeSpan.FromDays(this.authJWToptions.Value.Lifetime)),
                signingCredentials: new SigningCredentials(this.GetSymmetricSecurityKey(), SecurityAlgorithms.HmacSha256));

            var tokenRespModel = new AuthTokenResponseModel()
            {
                Token  = new JwtSecurityTokenHandler().WriteToken(jwt),
                UserId = Convert.ToInt32(claims.Name)
            };

            respModel.Item = tokenRespModel;

            return(respModel);
        }