public async Task <Dictionary <string, object> > Handle(AuthenticateUserQuery request, CancellationToken cancellationToken)
        {
            bool succes   = false;
            var  response = new Dictionary <string, object>();

            byte[] passwordBytes  = Encoding.ASCII.GetBytes(request.Password);
            SHA256 passwordSHA256 = SHA256.Create();

            byte[] hashValue    = passwordSHA256.ComputeHash(passwordBytes);
            string passwordHash = BitConverter.ToString(hashValue);

            passwordHash = passwordHash.Replace("-", "");
            var user = UserContext.Users.Where(u => u.Password == passwordHash && u.UserName == request.Username).FirstOrDefault();

            string tokenData;

            if (user != null)
            {
                var tokenHandler = new JwtSecurityTokenHandler();
                var key          = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration.GetSection("TokenKeys").GetSection("DefaultKey").Value));
                var credentials  = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
                var claims       = new[]
                {
                    new Claim("Username",
                              "Password")
                };
                var token = new JwtSecurityToken("https://localhost:5001", "https://localhost:5001", claims, DateTime.UtcNow, expires: DateTime.UtcNow.AddMinutes(30), signingCredentials: credentials);
                tokenData = new JwtSecurityTokenHandler().WriteToken(token);
                succes    = true;
                response.Add("Token", tokenData);
            }
            response.Add("succes", succes);
            response.Add("user", user);
            return(response);
        }
        public dynamic Authenticate([FromBody] dynamic inputs)
        {
            string username = inputs.Username;

            logger.Debug("Checking authentication for user : " + username);
            return(AuthenticateUserQuery.AuthenticateUser(username));
        }
示例#3
0
 public async Task <AuthenticateUserQueryResult> AuthenticateUser(AuthenticateUserQuery query)
 {
     return(await dispatcher.Dispatch <AuthenticateUserQuery, AuthenticateUserQueryResult>(
                new AuthenticateUserQuery()
     {
         Login = query.Login, Password = query.Password
     }));
 }
        public async Task <IActionResult> Login([FromBody] AuthenticateUserQuery query)
        {
            var result = await _queryExecutor.ExecuteAsync <AuthenticateUserQuery, User>(query);

            if (result.Success)
            {
                var token = GenerateJsonWebToken(result.Data);
                return(Ok(new { token = token }));
            }

            return(BadRequest(new { success = false }));
        }
        public async Task <Dictionary <string, object> > Handle(AuthenticateUserQuery request, CancellationToken cancellationToken)
        {
            var response = new Dictionary <string, object>();
            var user     = UserContext.Users.Where(u => u.UserName == request.Username && u.Password == request.Password).FirstOrDefault();

            if (user != null)
            {
                response.Add("authentication", "succes");
            }
            else
            {
                response.Add("authentication", "failed");
            }
            response.Add("id", user.Id);
            return(response);
        }
示例#6
0
        public async Task <IActionResult> Authenticate([FromBody] AuthenticateUserQuery query)
        {
            try
            {
                var user = await _mediator.Send(query);

                if (user == null)
                {
                    return(BadRequest("Username or password incorrect!"));
                }

                return(Ok(new { Email = user.Value.email, Token = user.Value.token }));
            }
            catch (Exception exception)
            {
                return(BadRequest($"User Login Error : {exception.Message}"));
            }
        }
示例#7
0
 public Task <AuthenticateUserResult> Authenticate(AuthenticateUserQuery query)
 {
     return(Mediator.Send(query));
 }