Exemple #1
0
        public IActionResult TokenKey(TokenEmail model)
        {
            // verify user
            var user = _userManager.Users.FirstOrDefault(x => x.Email == model.Email);

            if (user == null)
            {
                return(NotFound());
            }
            // claims
            var claims = new[]
            {
                new Claim(ClaimTypes.NameIdentifier, user.Id),
                new Claim(ClaimTypes.Email, user.Email)
            };
            // key
            var key   = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config.GetSection("AppSettings:Token").Value));
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature);
            var securityTokenDescriptor = new SecurityTokenDescriptor

            {
                Subject            = new ClaimsIdentity(claims),
                Expires            = DateTime.Now.AddDays(1),
                SigningCredentials = creds,
            };
            // token
            var tokenHandler = new JwtSecurityTokenHandler();
            var token        = tokenHandler.CreateToken(securityTokenDescriptor);

            // return token
            return(Ok(new { token = tokenHandler.WriteToken(token) }));
        }
        public async Task <IActionResult> DeleteUser(TokenEmail model)
        {
            // get and  delete user by email
            var result = await _userepo.DeleteUser(model);

            // check if deleted
            if (result == true)
            {
                return(Ok());
            }
            // return bad request
            return(BadRequest());
        }
Exemple #3
0
        public IActionResult NewToken([FromBody] TokenEmail input)
        {
            if (input.Email is null)
            {
                return(BadRequest("No email provided."));
            }

            try {
                // Not adding the secret message here, in case you accidentally see it.
                var    token         = InternetToken.CreateAesEcb(input.Email, "user");
                string jsonHexString = token.ToTokenHexString();
                return(Ok(jsonHexString));
            }
            catch (Exception e) {
                return(BadRequest(e.Message));
            }
        }
Exemple #4
0
        public async Task <bool> DeleteUser(TokenEmail model)
        {
            var user = await _userManager.FindByEmailAsync(model.Email);

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

            var result = await _userManager.DeleteAsync(user);

            if (result.Succeeded)
            {
                return(true);
            }

            return(false);
        }