申し訳ありませんが、これはかなり時間がかかっていましたが、私のサーバー上のクライアント認証用にクライアント証明書をWebリクエストで送信することができました。
まず、Xamarinと.netCoreは素晴らしいですが、それらは多くの方法が欠けています.netの開発者は慣れています。私はHttpWebRequestのようなAndroidとiosの両方で動作するクロスプラットフォーム要求を構築できませんでした。
私はその後、上書きNSUrlConnectionDataDelegate
:
//cert is a byte array of a .pfx file included in the resource file
//iSecurity Custom class
NSUrlCredential credential = iSecurity.ImportPK12File(cert, "certpassword");
public static NSUrlCredential ImportPK12File(byte[] fileBytes, string passPhrase)
{
var cert = new X509Certificate2(fileBytes, passPhrase);
var options = NSDictionary.FromObjectAndKey(NSObject.FromObject(passPhrase), SecImportExport.Passphrase);
NSDictionary[] importStatus;
SecStatusCode statusCode = SecImportExport.ImportPkcs12(fileBytes, options, out importStatus);
if(statusCode != SecStatusCode.Success){
throw new Exception("Error importing certificate. ");
}
NSObject obj = importStatus[0]["trust"];
IntPtr secTrustRef = obj.Handle;
var identityHandle = importStatus[0][SecImportExport.Identity];
var identity = new SecIdentity(identityHandle.Handle);
var certificate = new SecCertificate(cert.GetRawCertData());
SecCertificate[] certificates = { certificate };
return NSUrlCredential.FromIdentityCertificatesPersistance(identity, certificates, NSUrlCredentialPersistence.ForSession);
}
:私はその後、資格を返すクラスを作成し
public override void WillSendRequestForAuthenticationChallenge(NSUrlConnection
connection, NSUrlAuthenticationChallenge challenge)
{
byte[] cert = System.IO.File.ReadAllBytes("clientCertificate.pfx");
NSUrlCredential credential = iSecurity.ImportPK12File(cert, "certPassword");
challenge.Sender.UseCredential(credential, challenge);
}
iOS用
は、私から継承するカスタムクラスを作成しました
この方法を無効にして、信用状を送信することもできます: 公開オーバーライドvoid ReceivedAuthenticationChallenge(NSUrlConnection接続、NSUrlAuthenticationChallengeチャレンジ) { base.ReceivedAuthenticationChallenge(接続、チャレンジ); }
と私はそこにいますが、から継承クラスのデリゲート作成し、これをオフに発射するためにそれを移動することがあります。NSUrlConnectionDataDelegate をし、あなたの接続にこれを追加します。この接続を介して起動されたリクエストは、メソッドをオーバーライドして証明書を渡します。
深い掘り下げが行われた後、クライアント証明書はミューテータに設定できますが、iosでは実装されません。 – Dys1