(https://github.com/dvsekhvalnov/jose-jwt参照)を使用してAppleの新しいトークンベースのAPNシステムで使用するJWTトークンを生成しようとしています。CryptKeyをインポートメソッドで.p8ファイルから作成しようとしました。「エンコードまたはデコード操作中にエラーが発生しました。」というエラーが発生しました。
JWTエンコード方式では、secretKeyがCngKey
の形式である必要があります。
var privateKeyContent = System.IO.File.ReadAllText(authKeyPath);
var privateKey = privateKeyContent.Split('\n')[1];
//convert the private key to CngKey object and generate JWT
var secretKeyFile = Convert.FromBase64String(privateKey);
var secretKey = CngKey.Import(secretKeyFile, CngKeyBlobFormat.Pkcs8PrivateBlob);
しかし、最後の行に、次のエラーがスローされます。 はここCngKey
オブジェクトにアップルから.p8ファイルを変換する私のコードです。
System.Security.Cryptography.CryptographicException was unhandled by user code
HResult=-2146885630
Message=An error occurred during encode or decode operation.
Source=System.Core
StackTrace:
at System.Security.Cryptography.NCryptNative.ImportKey(SafeNCryptProviderHandle provider, Byte[] keyBlob, String format)
at System.Security.Cryptography.CngKey.Import(Byte[] keyBlob, String curveName, CngKeyBlobFormat format, CngProvider provider)
at System.Security.Cryptography.CngKey.Import(Byte[] keyBlob, CngKeyBlobFormat format)
at tokenauthapi.App_Start.TokenInitSendMessage.<send>d__0.MoveNext() in C:\token-push-prototype\token-auth-api\token-auth-api\App_Start\TokenInitSendMessage.cs:line 31
InnerException:
(私はブロブの種類を変更したときに表示される)そのため、別のエラーがありますように、入力が間違った形式ではありません。
このコードは.NET WebApi v4.6で実行されています。
私は高低を検索しましたが、このエラーの意味を解読できませんでした。どんな助けでも大歓迎です。ありがとうございました。