@@ -49,7 +49,29 @@ public static IBankIdBuilder UseClientCertificate(this IBankIdBuilder builder, F
4949 void ConfigureHttpClientHandler ( IServiceProvider sp , SocketsHttpHandler httpClientHandler )
5050 {
5151 var clientCertificate = configureClientCertificate ( ) ;
52- httpClientHandler . SslOptions . ClientCertificates = new X509Certificate2Collection { clientCertificate } ;
52+ httpClientHandler . SslOptions . ClientCertificates ??= new X509Certificate2Collection ( ) ;
53+ httpClientHandler . SslOptions . ClientCertificates . Add ( clientCertificate ) ;
54+ }
55+ }
56+
57+ /// <summary>
58+ /// Add client certificate for authenticating against the BankID API to the list of available certificates for the http client handler to choose from.
59+ /// </summary>
60+ /// <param name="builder"></param>
61+ /// <param name="configureClientCertificate">The certificate to add.</param>
62+ /// <returns></returns>
63+ public static IBankIdBuilder UseClientCertificate ( this IBankIdBuilder builder , Func < IServiceProvider , X509Certificate2 > configureClientCertificate )
64+ {
65+ builder . ConfigureAppApiHttpClientHandler ( ConfigureHttpClientHandler ) ;
66+ builder . ConfigureVerifyApiHttpClientHandler ( ConfigureHttpClientHandler ) ;
67+
68+ return builder ;
69+
70+ void ConfigureHttpClientHandler ( IServiceProvider sp , SocketsHttpHandler httpClientHandler )
71+ {
72+ var clientCertificate = configureClientCertificate ( sp ) ;
73+ httpClientHandler . SslOptions . ClientCertificates ??= new X509Certificate2Collection ( ) ;
74+ httpClientHandler . SslOptions . ClientCertificates . Add ( clientCertificate ) ;
5375 }
5476 }
5577
@@ -186,7 +208,12 @@ internal static IBankIdBuilder UseEnvironment(this IBankIdBuilder builder, Uri a
186208 /// <param name="useBankIdClientCertificate">Use the BankID client certificate (for test) from the BankID documentation.</param>
187209 /// <param name="clientCertificateFormat">If using the BankID client certificate (for test). Select the preferred format p12, pem or pfx.</param>
188210 /// <returns></returns>
189- public static IBankIdBuilder UseTestEnvironment ( this IBankIdBuilder builder , bool useBankIdRootCertificate = true , bool useBankIdClientCertificate = true , TestCertificateFormat clientCertificateFormat = TestCertificateFormat . PFX )
211+ public static IBankIdBuilder UseTestEnvironment (
212+ this IBankIdBuilder builder ,
213+ bool useBankIdRootCertificate = true ,
214+ bool useBankIdClientCertificate = true ,
215+ TestCertificateFormat clientCertificateFormat = TestCertificateFormat . PFX
216+ )
190217 {
191218 builder . UseEnvironment ( BankIdUrls . AppApiTestBaseUrl , BankIdUrls . VerifyApiTestBaseUrl , BankIdEnvironments . Test ) ;
192219 builder . Services . AddTransient < IBankIdCertificatePolicyResolver , BankIdCertificatePolicyResolverForTest > ( ) ;
0 commit comments