ShoppingApp/Weifer.ShoppingApp.API/Controllers/AuthenticationController.cs

34 lines
1.1 KiB
C#
Raw Permalink Normal View History

using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Text;
namespace Weifer.ShoppingApp.API.Controllers
2024-02-25 13:22:48 +01:00
{
public class AuthenticationController
{
public string HashPassword(string password)
{
string hashedPassword = BCrypt.Net.BCrypt.HashPassword(password);
return hashedPassword;
}
public bool VerifyPassword(string password, string hashedPassword)
{
return BCrypt.Net.BCrypt.Verify(password, hashedPassword);
}
public string GenerateJwtToken()
{
var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("weifer.shoppingapp"));
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(
issuer: "localhost",
audience: "localhost",
expires: DateTime.Now.AddMinutes(15),
signingCredentials: credentials);
return new JwtSecurityTokenHandler().WriteToken(token);
2024-02-25 13:22:48 +01:00
}
}
}