34 lines
1.1 KiB
C#
34 lines
1.1 KiB
C#
using Microsoft.IdentityModel.Tokens;
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Text;
|
|
|
|
namespace Weifer.ShoppingApp.API.Controllers
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
}
|