I'm tiring to generate the jwt token for calling apns. here is my code:
var header = new Dictionary<string, object>()
{
{ "kid" , keyID }
};
var payload = new Dictionary<string, object>()
{
{ "iss", teamID },
{ "iat", DateTimeOffset.Now.ToUnixTimeSeconds().ToString() }
};
var privateKey = GetApnsPrivateKey(authKeyPath);
var token = JWT.Encode(payload, privateKey, JwsAlgorithm.ES256, header);
public static CngKey GetApnsPrivateKey(string authKeyPath)
{
using (var reader = new StreamReader(new FileStream(authKeyPath, FileMode.Open, FileAccess.Read, FileShare.Read)))
{
var ecPrivateKeyParameters = (ECPrivateKeyParameters)new PemReader(reader).ReadObject();
var x = ecPrivateKeyParameters.Parameters.G.AffineXCoord.GetEncoded();
var y = ecPrivateKeyParameters.Parameters.G.AffineYCoord.GetEncoded();
var d = ecPrivateKeyParameters.D.ToByteArrayUnsigned();
return EccKey.New(x, y, d);
code works on my machine just fine but in the server it throw this exception on GetApnsPrivateKey
method:
System.Security.Cryptography.CryptographicException: The requested operation is not supported
after little search on the forums i found out its because of CNG Key Storage Providers: https://msdn.microsoft.com/en-us/library/windows/desktop/aa376242.aspx?f=255&MSPPError=-2147217396 now i wonder is there any way to solve the problem?