public async Task <dynamic> OnPostAsync([FromForm] string id, [FromForm] string password)
        {
            try
            {
                var user = await this.userService.AuthenticateAsync(id, password);

                var token = await tokenService.AddTokenAsync(new Token(user.Id, user.Role, 24));

                return(new { Role = user.Role, Token = token });
            }
            catch (Exception e)
            {
                return(new { Role = UserRole.Unknown, Error = e.Message });
            }
        }
Пример #2
0
        public async Task <IActionResult> LoginAsync(Client clientInfo)
        {
            try
            {
                var client = await clientDbContext.Clients.FindAsync(clientInfo.Id);

                if (client == null)
                {
                    throw new Exception("User Not Found!");
                }
                if (client.Password != clientInfo.Password)
                {
                    throw new Exception("Incorrect Password!");
                }
                var token = await tokenService.AddTokenAsync(new Token(client.Id, 24));

                return(Ok(new { Res = true, Token = token }));
            }
            catch (Exception e)
            {
                logger.LogError(e.Message);
                return(BadRequest(new { Res = false, Error = e.Message }));
            }
        }