Jwt Token Manager

 public string CreateToken(int branchID, string employeeID)

{

var tokenString = "";

try

{

var secretKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("myEncryptionKey@143#"));


var signinCredentials = new SigningCredentials(secretKey, SecurityAlgorithms.HmacSha256);

var claims = new List<Claim>

{

new Claim(ClaimTypes.Name,  branchID + "," + employeeID)

};

var tokeOptions = new JwtSecurityToken(

issuer: "http://localhost:5000",

audience: "http://localhost:5000",

claims: claims,

notBefore: DateTime.Now,

signingCredentials: signinCredentials

);

tokenString = new JwtSecurityTokenHandler().WriteToken(tokeOptions);

}

catch (Exception e) { }


return tokenString;

}



public string CreatePaymentToken(string policyId, string CustomerID)

{

var tokenString = "";

try

{

//var symmetricKey = Convert.FromBase64String("bXlFbmNyeXB0aW9uS2V5QDE0MyM=");

//var SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(symmetricKey), SecurityAlgorithms.HmacSha256Signature);

//var claims = new List<Claim>

// {

// new Claim(ClaimTypes.Name, policyId +","+ CustomerID )

//     };

//var tokeOptions = new JwtSecurityToken(

//issuer: "http://localhost:5000",

//audience: "http://localhost:5000",

//claims: claims,

//notBefore: DateTime.Now,

//signingCredentials: SigningCredentials

//);

//tokenString = new JwtSecurityTokenHandler().WriteToken(tokeOptions);

string Key =("bXlFbmNyeXB0aW9uS2V5QDE0MyM=");

tokenString = Base64Encode(policyId + "-" + Key+"-"+ CustomerID);

}

catch (Exception e) { }


return tokenString;

}

public  string Base64Encode(string plainText)

{

var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);

return System.Convert.ToBase64String(plainTextBytes);

}

public  string Base64Decode(string base64EncodedData)

{

var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);

return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);

}

No comments:

Post a Comment