Esempio n. 1
0
        public async Task <IActionResult> Login([FromBody] UserLoginDto user)
        {
            //throw new System.Exception("Error Test");

            var userFromRepo = await repo.Login(user.Username.ToLower(), user.Password);

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

            var tokenString = JWTUtils.CreateToken(userFromRepo, conf);

            return(Ok(new { tokenString }));
        }
        public async Task <IActionResult> Login(UserLoginDto userLoginDto)
        {
            var dbUser = await _authRepository.Login(userLoginDto.UserName.ToLowerInvariant(), userLoginDto.Password);

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

            var user = _mapper.Map <UserForListDto>(dbUser);

            var token = _tokenService.GenerateToken(dbUser.Id, dbUser.UserName);

            return(Ok(new { token, user }));
        }
        public async Task <IActionResult> Login([FromBody] UserLoginDto userDto)
        {
            var user = await _userManager.FindByNameAsync(userDto.Username);

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

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

                var userToReturn = _mapper.Map <UserListDto>(appUser);

                return(Ok(new { token = GenerateJwtToken(appUser), user = userToReturn }));
            }
            return(Unauthorized());
        }
Esempio n. 4
0
        public async Task <IActionResult> Login(UserLoginDto userLogin)
        {
            // Function attempts to log in the user account

            User userAccount = await _repo.Login(userLogin.Username, userLogin.Password);

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

            // Fetch a JWT Token for this user account
            return(Ok(new {
                token = GenerateJwtToken(userAccount.Id.ToString(), userAccount.Username)
            }));
        }
Esempio n. 5
0
        public async Task <IActionResult> Login(UserLoginDto userLoginDto)
        {
            //Getting the user from repo once login is successfull
            var userFromRepo = await _authRepository.Login(userLoginDto.UserName.ToLower(), userLoginDto.Password);

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

            //Creating a secutiy claim based on the UserId and UserName so that once the user is authentictaed
            //next time it can work with authorized token by looking into the token and getting the details
            //of username and id
            //Crating the cliam
            var claims = new[]
            {
                new Claim(ClaimTypes.NameIdentifier, userFromRepo.Id.ToString()),
                new Claim(ClaimTypes.Name, userFromRepo.UserName)
            };

            //Creating the key for hashing the generated token
            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config.GetSection("AppSettings:Token").Value));

            //Signing the key
            var credential = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature);

            //Adding the expiry for token
            var tokenDecriptor = new SecurityTokenDescriptor
            {
                SigningCredentials = credential,
                Subject            = new ClaimsIdentity(claims),
                Expires            = DateTime.Now.AddDays(1)
            };

            //token handler
            var tokenHandler = new JwtSecurityTokenHandler();

            //Creating the token with the help of token handler
            var token = tokenHandler.CreateToken(tokenDecriptor);

            return(Ok(new
            {
                token = tokenHandler.WriteToken(token)
            }));
        }
Esempio n. 6
0
        public async Task <IActionResult> LoginToken(UserLoginDto userLogin)
        {
            var userData = await _repo.Login(userLogin.UserName, userLogin.Password);

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

            // variable to store claims so to get user info no need to get from DB again. claim will be fill in token
            var claims = new[] {
                new Claim(ClaimTypes.NameIdentifier, userData.Id.ToString()),
                new Claim(ClaimTypes.Name, userData.UserName)
            };

            // encode key to sign the token, signing key is to make sure the token on the next request from client is valid token
            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config.GetSection("AppSettings:Token").Value));
            // signing and encript the key as cridential with algorithm to hash
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature);

            // create security token descriptor that contain claims, signing credential, exp token date
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject            = new ClaimsIdentity(claims),
                Expires            = DateTime.Now.AddDays(1),
                SigningCredentials = creds
            };

            // to validate token discriptor we need token handler
            var tokenHandler = new JwtSecurityTokenHandler();
            // using token handler we create roken and pass the descriptor, this token that returned to the client
            var token = tokenHandler.CreateToken(tokenDescriptor);

            var user = _mapper.Map <UserListDto>(userData);

            // before returned to client serialized token
            return(Ok(new
            {
                token = tokenHandler.WriteToken(token),
                user
            }));

            // to encode or decode token you can visit https://jwt.io
        }
        public async Task <IActionResult> Login(UserLoginDto model)
        {
            if (ModelState.IsValid)
            {
                var userLogin = await _repo.Login(model.username.ToLower(), model.Password);

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

                // Claim claim = new Claim(ClaimTypes.NameIdentifier, userLogin.id.ToString());

                var claims = new[]
                {
                    new Claim(ClaimTypes.NameIdentifier, userLogin.id.ToString()),
                    new Claim(ClaimTypes.Name, userLogin.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
                };

                JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();

                var token = tokenHandler.CreateToken(tokenDescriptor);

                var handlerToken = tokenHandler.WriteToken(token);

                return(Ok(new { handlerToken }));
            }
            else
            {
                return(Unauthorized());
            }
        }
Esempio n. 8
0
        public async Task <IActionResult> Login(UserLoginDto userToLogin)
        {
            //throw new Exception("computer dice no!!");
            userToLogin.Username = userToLogin.Username.ToLower();
            var userFromRepo = await _repo.Login(userToLogin.Username, userToLogin.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
            }));
        }
Esempio n. 9
0
        public async Task <IActionResult> Login(UserLoginDto userLoginDto)
        {
            // var userFromRepo = await _repo.Login(userLoginDto.Username.ToLower(), userLoginDto.Password);
            var user = await _userManager.FindByNameAsync(userLoginDto.Username);

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

            // if (userFromRepo == null)
            //     return Unauthorized();
            if (result.Succeeded)
            {
                var appUser = _mapper.Map <UserForListDto>(user);
                return(Ok(new
                {
                    token = await GenerateJwtToken(user),
                    user = appUser
                }));
            }
            return(Unauthorized());
        }
Esempio n. 10
0
        public async Task <IActionResult> Login(UserLoginDto userLoginDto)
        {
            var userFromRepo = await _repo.Login(userLoginDto.Username, userLoginDto.Password);

            //throw new Exception("Computer says no!");

            if (userFromRepo == null)
            {
                return(Unauthorized());
            }
            Console.WriteLine($"Testing {userFromRepo.Id}");
            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);

            Console.WriteLine(token);
            Console.WriteLine(tokenHandler.WriteToken(token));
            return(Ok(new {
                token = tokenHandler.WriteToken(token)
            }));
        }
Esempio n. 11
0
        public async Task <IActionResult> Login(UserLoginDto model)
        {
            var userFromRepo = await authRepository.Login(model.UserName, model.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(configuration.GetSection("AppSettings:SecretKey").Value));

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

            JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();
            SecurityToken           token        = tokenHandler.CreateToken(tokenDescriptor);

            var user        = _mapper.Map <UserForListDto>(userFromRepo);
            var tokenReturn = tokenHandler.WriteToken(token);

            Console.Write(JsonConvert.SerializeObject(user));

            return(Ok(JsonConvert.SerializeObject(new
            {
                token = tokenHandler.WriteToken(token),
                user
            })));
        }