コード例 #1
0
        private UserToken BuildToken(UserInDTO userInDTO)
        {
            var claims = new List <Claim>()
            {
                new Claim(JwtRegisteredClaimNames.UniqueName, userInDTO.Email),
                new Claim("Mi Key", "Mi Valor"),
                new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
            };

            var key        = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["SecretKey"]));
            var creds      = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            var expiration = DateTime.UtcNow.AddMinutes(5);

            var token = new JwtSecurityToken(
                issuer: null,
                audience: null,
                claims: claims,
                signingCredentials: creds,
                expires: expiration
                );

            return(new UserToken
            {
                Token = new JwtSecurityTokenHandler().WriteToken(token).ToString(),
                Expiration = expiration.ToString("dd/MM/yyyy hh:mm:ss")
            });
        }
コード例 #2
0
        public async Task <ActionResult <UserToken> > LoginAsync(UserInDTO userInDTO)
        {
            var result = await _signInManager.PasswordSignInAsync(userInDTO.Email, userInDTO.Password, isPersistent : false, lockoutOnFailure : false);

            if (result.Succeeded)
            {
                return(BuildToken(userInDTO));
            }
            return(BadRequest("Login failed attempt."));
        }
コード例 #3
0
        public async Task <ActionResult <UserToken> > CreateAsync(UserInDTO userInDTO)
        {
            var user = new ApplicationUser()
            {
                UserName = userInDTO.Email, Email = userInDTO.Email
            };
            var result = await _userManager.CreateAsync(user, userInDTO.Password);

            if (result.Succeeded)
            {
                return(BuildToken(userInDTO));
            }
            return(BadRequest(result.Errors));
        }