Xamarin Android - 使用带有证书的 HttpClient 调用 API [英] Xamarin Android - Call API using HttpClient with Certificate

查看:30
本文介绍了Xamarin Android - 使用带有证书的 HttpClient 调用 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试与我们的 API 进行通信,该 API 在请求中需要证书,但效果不佳.

We're trying to communicate with an API of ours which needs a certificate in the request but it doesn't work well.

通常我们只是通过在请求标头中传递一个 SubscriptionKey 来使用外部服务或 Azure API 管理 API,但这次我们有一个证书 (.cer),我们需要在 AndroidHttpClientHandler 中注入,但此功能没有似乎受到支持,或者我们使用错误?

Usually we used an external service or an Azure API Management API just by passing a SubscriptionKey in the request headers, but this time we have a certificate (.cer) that we need to inject in the AndroidHttpClientHandler but this feature doesn't seem to be supported, or maybe we're using it wrong ?

你知道这个场景是否真的可能,如果是,我们应该如何实现它?我尝试了很多方法,例如创建自定义 AndroidHttpClientHandler 以允许在其中放置 ClientCertificate 但没有成功

Do you know if this scenario is truly possible and if yes, how we should achieve it ? I tried many things such as create a custom AndroidHttpClientHandler to allow putting a ClientCertificate in it but without success

推荐答案

终于成功了 :D 对于那些有兴趣的人,我是如何实现它的 :

Finally managed to get it work :D For those who are interested, here's how I achieve it :

TL;DR:升级到 Visual Studio 2019 和 HttpWebRequest 来救援(您可以在下面找到代码示例);-)

上下文:我们的项目由一个 PCL 和一个 Android 项目组成.在团队中,我们知道我们必须将 PCL 迁移到 .NET Standard 项目,但这需要时间,尤其是当您有很多库要处理时(未更新到 .NET Standard 的库)xP

Context: our project consists of a PCL and a Android project. In the team, we know we have to migrate the PCL to a .NET Standard project but it takes time, especially when you have a lot of librairies to deal with (libraries that are not updated to .NET Standard) xP

  • 当你想调用一个 API 时,你首先想到的是使用 HttpClient/HttpRequestHandler 对,我们只需要在 HttpRequestHandler 中传递我们的证书,如下所示:

  • When you want to call an API, the first thing that comes to your mind is to use the HttpClient/HttpRequestHandler pair where we just have to pass our certificate in the HttpRequestHandler as follows :

httpRequestHandler.ClientCertificates.Add(new X509Certificate2(..))

httpRequestHandler.ClientCertificates.Add(new X509Certificate2(..))

为什么它不起作用?因为我们正在开发 Xamarin.Android,它在 Mono.Droid 引擎盖下使用,因此我们遇到了不受欢迎的 NotImplementedException() !WebRequestHandler 呢?同样的命运 :P

Why it didn't work ? Because we're developing with Xamarin.Android which uses under the hood Mono.Droid, therefore we're meeting the unpopular NotImplementedException() ! What about WebRequestHandler ? Well the same fate :P

希望来自 HttpWebRequest 的拯救如下:

Hopefully the salvation came from HttpWebRequest as follows :

        private Task<string> ExecuteRequest(Uri uri, X509Certificate2 certificate)
        {
            // Create a web request that points to our secured Backend API
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);

            if (certificate != null)
            {
                // Associate the certificates with the request
                request.ClientCertificates.Add(certificate);
            }

            // Launch the web request
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            // Output the stream to a jsonTextReader or anything else depending on your needs
            using (Stream stream = response.GetResponseStream())
            using (StreamReader sr = new StreamReader(stream))
            using (var jsonTextReader = new JsonTextReader(sr))
            {
                // Do whatever you want
            }
        }

此代码在我的机器(Visual Studio 2019)上有效,但在我的同事(Visual Studio 2017)上无效:确实遇到了以下异常:

This code worked on my machine (Visual Studio 2019) but not on my colleague (Visual Studio 2017) : indeed the following exception was met :

System.Security.Authentication.AuthenticationException:调用 SSPI 失败,请参阅内部异常.

我的机器上也安装了 VS2017,所以我尝试用它执行相同的代码,虽然听起来很奇怪,但我也遇到了错误

I also have VS2017 installed on my machine so I've tried to execute the same code with it and as strange as it sounds, I also got the error

等等 :) 当然,证书必须是嵌入式资源"

Et voilà :) Of course, the certificate had to be "an Embedded resource"

这篇关于Xamarin Android - 使用带有证书的 HttpClient 调用 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆