Last active
May 19, 2020 18:32
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.IdentityModel.Tokens; | |
using System; | |
using System.IdentityModel.Tokens.Jwt; | |
using System.Security.Claims; | |
namespace SwaggerWithJWT.Helpers | |
{ | |
public class JwtManager | |
{ | |
/// <summary> | |
/// Simetrik Gizli Anahtar oluşturmak için aşağıdaki kodu kullanılabilir. | |
/// var hmac = new HMACSHA256(); | |
/// var key = Convert.ToBase64String(hmac.Key); | |
/// </summary> | |
private const string Secret = "db3OIsj+BXE9NZDy0t8W3TcNekrF+2d/1sFnWG4HnV8TZY30iTOdtVWJG8abWvB1GlOgJuQZdcF2Luqm/hccMw=="; | |
public static string GenerateToken(string username, int expireMinutes = 240) | |
{ | |
var symmetricKey = Convert.FromBase64String(Secret); | |
var tokenHandler = new JwtSecurityTokenHandler(); | |
var now = DateTime.UtcNow; | |
var tokenDescriptor = new SecurityTokenDescriptor | |
{ | |
Subject = new ClaimsIdentity(new[] | |
{ | |
new Claim(ClaimTypes.Name, username), //Oturum açan kullanıcı adını ya da maili bu aşamada ekleniyor | |
new Claim(ClaimTypes.Role, "Role") // İstenirse rol/roller eklenebilir. | |
}), | |
Expires = now.AddMinutes(Convert.ToInt32(expireMinutes)), | |
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(symmetricKey), SecurityAlgorithms.HmacSha256Signature) | |
}; | |
SecurityToken securityToken = tokenHandler.CreateToken(tokenDescriptor); | |
var token = tokenHandler.WriteToken(securityToken); | |
return token; | |
} | |
public static ClaimsPrincipal GetPrincipal(string token) | |
{ | |
try | |
{ | |
var tokenHandler = new JwtSecurityTokenHandler(); | |
var jwtToken = tokenHandler.ReadToken(token) as JwtSecurityToken; | |
if (jwtToken == null) | |
return null; | |
var symmetricKey = Convert.FromBase64String(Secret); | |
var validationParameters = new TokenValidationParameters() | |
{ | |
RequireExpirationTime = true, | |
ValidateIssuer = false, | |
ValidateAudience = false, | |
LifetimeValidator = LifetimeValidator, //Token'nın geçerlilik zamanını kontrol ediyoruz. | |
IssuerSigningKey = new SymmetricSecurityKey(symmetricKey) | |
}; | |
var principal = tokenHandler.ValidateToken(token, validationParameters, out _); | |
return principal; | |
} | |
catch (Exception) | |
{ | |
return null; | |
} | |
} | |
//Token'nın geçerlilik zamanını kontrol ediyoruz. | |
static bool LifetimeValidator(DateTime? notBefore, DateTime? expires, SecurityToken securityToken, TokenValidationParameters validationParameters) | |
{ | |
if (expires != null) | |
{ | |
if (DateTime.UtcNow < expires) return true; | |
} | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment