Ejemplo n.º 1
0
        public async Task <IActionResult> Login(UserForLoginDTO userForLoginDTO)
        {
            var userFromRepo = await _repo.Login(userForLoginDTO.Username.ToLower(), userForLoginDTO.Password);

            if (userFromRepo == null)
            {
                return(Unauthorized());
            }

            var claims = new[] {
                new Claim(ClaimTypes.NameIdentifier, userFromRepo.Id.ToString()),
                new Claim(ClaimTypes.Name, userFromRepo.Username)
            };

            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config.GetSection("AppSettings:Token").Value));

            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature);

            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject            = new ClaimsIdentity(claims),
                Expires            = DateTime.Now.AddDays(1),
                SigningCredentials = creds
            };

            var tokenHandler = new JwtSecurityTokenHandler();

            var token = tokenHandler.CreateToken(tokenDescriptor);

            var user = _mapper.Map <UserForListDTO>(userFromRepo);

            return(Ok(new
            {
                token = tokenHandler.WriteToken(token),
                user
            }));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Login(UserForLoginDTO UserForLoginDTO)
        {
            // throw new Exception("Computer Says No");

            var User = await _repo.Login(UserForLoginDTO.Username, UserForLoginDTO.Password);

            if (User == null)
            {
                return(Unauthorized());
            }

            else
            {
                var Claims = new[]
                {
                    new Claim(ClaimTypes.NameIdentifier, User.Id.ToString()),
                    new Claim(ClaimTypes.Name, User.UserName)
                };

                var Key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config.GetSection("AppSetting:Token").Value));

                var Cred = new SigningCredentials(Key, SecurityAlgorithms.HmacSha512);

                var tokenDescriptor = new SecurityTokenDescriptor {
                    Subject            = new ClaimsIdentity(Claims),
                    SigningCredentials = Cred,
                    Expires            = DateTime.Now.AddDays(1)
                };

                var TokenHandller = new JwtSecurityTokenHandler();
                var Token         = TokenHandller.CreateToken(tokenDescriptor);

                return(Ok(new {
                    Token = TokenHandller.WriteToken(Token)
                }));
            }
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Login(UserForLoginDTO userForLoginDTO)
        {
            var user = await _userManager.FindByNameAsync(userForLoginDTO.Username);

            if (user == null)
            {
                return(Unauthorized());
            }

            var result = await _signInManager.CheckPasswordSignInAsync(user, userForLoginDTO.Password, false);

            if (result.Succeeded)
            {
                var userForListDTO = _mapper.Map <UserForListDTO>(user);

                return(Ok(new
                {
                    token = GenerateJwtToken(user).Result,
                    user = userForListDTO
                }));
            }

            return(Unauthorized());
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Login(UserForLoginDTO userForLoginDTO)
        {
            var user = await _userManager.FindByNameAsync(userForLoginDTO.Username);

            if (user == null)
            {
                return(Unauthorized());
            }
            var result = await _signInManager.CheckPasswordSignInAsync(user,
                                                                       userForLoginDTO.Password, false);

            if (result.Succeeded)
            {
                var appUser = await _userManager.Users.Include(p => p.Photos)
                              .FirstOrDefaultAsync(u => u.NormalizedUserName == userForLoginDTO.Username.ToUpper());

                var userToReturn = _mapper.Map <UserForListDTO>(appUser);
                return(Ok(new {
                    token = GenerateJwtToken(appUser).Result,
                    user = userToReturn
                }));
            }
            return(Unauthorized());
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Login(UserForLoginDTO userForLoginDTO)
        {
            // validate request
            var userFromRepo = await _repo.Login(userForLoginDTO.Username, userForLoginDTO.Password);

            if (userFromRepo is null)
            {
                return(Unauthorized());
            }

            //build the token
            var claims = new[]
            {
                new Claim(ClaimTypes.NameIdentifier, userFromRepo.Id.ToString()),
                new Claim(ClaimTypes.Name, userFromRepo.Username)
            };

            var key = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(_config.GetSection("AppSettings:Token").Value));

            var creds = new Microsoft.IdentityModel.Tokens.SigningCredentials(key, Microsoft.IdentityModel.Tokens.SecurityAlgorithms.HmacSha512Signature);

            var tokenDescriptor = new Microsoft.IdentityModel.Tokens.SecurityTokenDescriptor
            {
                Subject            = new ClaimsIdentity(claims),
                Expires            = DateTime.Now.AddDays(1),
                SigningCredentials = creds
            };

            var tokenHandler = new JwtSecurityTokenHandler();

            var token = tokenHandler.CreateToken(tokenDescriptor);

            return(Ok(new {
                token = tokenHandler.WriteToken(token)
            }));
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> Login(UserForLoginDTO userForLoginDTO)
        {
            var user = await _userManager.Users.Include(p => p.Photos).SingleOrDefaultAsync(u => u.UserName.ToLower() == userForLoginDTO.Username.ToLower());

            if (user == null)
            {
                return(Unauthorized());
            }

            var result = await _signInManager.CheckPasswordSignInAsync(user, userForLoginDTO.Password, false);

            if (result.Succeeded)
            {
                var appUser = _mapper.Map <UserForNavbarDTO>(user);

                return(Ok(new
                {
                    token = GenerateJwtToken(user).Result,
                    user = appUser
                }));
            }

            return(Unauthorized());
        }