Exemplo n.º 1
0
        private async Task <ClaimsIdentity> GetClaimsIdentity(string userName, string password)
        {
            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
            {
                return(null);
            }

            var userToVerify = await _userManager.FindByNameAsync(userName);

            if (userToVerify == null)
            {
                return(null);
            }

            if (await _userManager.CheckPasswordAsync(userToVerify, password))
            {
                return(_jwtGenerator.GenerateClaimsIdentity(userName, userToVerify.Id));
            }

            return(null);
        }
Exemplo n.º 2
0
        private async Task <ClaimsIdentity> GetClaimsIdentity(string emailAddress, string password)
        {
            // Checks if one of the fields is null to begin with
            if (string.IsNullOrEmpty(emailAddress) || string.IsNullOrEmpty(password))
            {
                return(await Task.FromResult <ClaimsIdentity>(null));
            }

            // Get the user to verifty
            var userToVerify = (from user in _context.Users
                                where user.EmailAddress == emailAddress && user.UserPassword == password
                                select Tuple.Create(user.Id, user.Role)).ToArray();


            if (userToVerify.Length != 0)
            {
                return(await Task.FromResult(_jwtGenerator.GenerateClaimsIdentity(emailAddress, userToVerify[0].Item1.ToString(), userToVerify[0].Item2.ToString())));
            }

            // Credentials are invalid, or account doesn't exist
            return(await Task.FromResult <ClaimsIdentity>(null));
        }
Exemplo n.º 3
0
        public ActionResult Token(LoginModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var username = model.Email;
            var password = model.Password;

            var user     = repository.FindOne(x => x.Email == username && x.Password == password);
            var identity = jwtGenerator.GenerateClaimsIdentity(user);

            if (identity == null)
            {
                return(BadRequest("Invalid username or password."));
            }

            var encodedJwt = jwtGenerator.GenerateEncodedToken(identity);
            var role       = identity.Claims.Where(c => c.Type == ClaimTypes.Role).Select(c => c.Value).First();
            var response   = new ResponceLogin(encodedJwt, identity.Name, role, user.Avatar);

            return(Ok(response));
        }