Esempio n. 1
0
        public async Task <IActionResult> CreateToken([FromBody] CredentialModel model)
        {
            try
            {
                var user = await userManager.FindByNameAsync(model.UserName);

                if (user != null)
                {
                    if (hasher.VerifyHashedPassword(user, user.PasswordHash, model.Password) == PasswordVerificationResult.Success)
                    {
                        var claims = await userManager.GetClaimsAsync(user);

                        var key   = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("verylongkeyvaluethatissecured"));
                        var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
                        var token = new JwtSecurityToken(
                            issuer: "issuer",
                            audience: "audience",
                            claims: claims,
                            expires: DateTime.UtcNow.AddMinutes(15),
                            signingCredentials: creds);

                        return(Ok(new
                        {
                            token = new JwtSecurityTokenHandler().WriteToken(token),
                            expiration = token.ValidTo
                        }));
                    }
                }
                return(BadRequest());
            }
            catch (Exception)
            {
                return(BadRequest());
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> Login([FromBody] CredentialModel model)
        {
            try
            {
                var result = await signInManager.PasswordSignInAsync(model.UserName, model.Password, false, false);

                if (result.Succeeded)
                {
                    return(Ok());
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            return(BadRequest());
        }