1. 程式人生 > >ASP.NET Sign In With Apple 後端驗證(C#)

ASP.NET Sign In With Apple 後端驗證(C#)

蘋果在2019年 9 月12 號更新了稽核指南,加入 4.8 Sign in with Apple 一條,要求所有使用第三方登入 的 App,都必須接入 Sign in with Apple。已經上架的 App 需在 2020 年 4 月 前完成接入工作,新上架 App(如果支援三方登入)必須接入Sign in with Apple,否則將被拒。 App登入成功後,需要將獲取到的 identityToken、code等資訊傳送給後臺,然後由後臺呼叫 Apple 的後臺API,來驗證使用者的真實性,從而完成驗證。 本文講述C#基於授權碼的Sign In With Apple後端驗證: ###### client_secret的構建方法 先在後臺生成授權應用APP ID的金鑰KEY檔案,然後下載金鑰檔案,此檔案只能下載一次,請妥善儲存,格式樣例: ``` #金鑰KEY格式樣例 -----BEGIN PRIVATE KEY----- BASE64編碼後的金鑰 -----END PRIVATE KEY----- ``` 祕鑰讀取 ``` /// /// 獲取P8 /// /// private CngKey GetPrivateKey() { const string privateKey = @"BASE64編碼後的金鑰"; // contents of .p8 file var cngKey = CngKey.Import( Convert.FromBase64String(privateKey), CngKeyBlobFormat.Pkcs8PrivateBlob); return cngKey; } private static SigningCredentials CreateSigningCredentials(string keyId, ECDsa algorithm) { var key = new ECDsaSecurityKey(algorithm) { KeyId = keyId }; return new SigningCredentials(key, SecurityAlgorithms.EcdsaSha256Signature); } ``` ###### 驗證 ``` /// /// 檢驗生成的授權碼是正確的,需要給出正確的授權碼 ///
/// 授權碼 /// apple使用者ID /// public async Task