使用HttpClient时如何处理多个证书

25

我正在开发一个Windows Phone 8.1应用程序,允许Azure用户使用Azure服务管理API查看其订阅/服务。身份验证是使用管理证书完成的,并且该证书附加到对API的所有请求中。单个用户可以正常使用,但当我尝试添加多个订阅功能时出现问题。我能够将证书安装在证书存储中并检索它,但是当我向API发送请求时出现问题。尽管我附加了正确的证书,但我收到403禁止错误。 以下是我使用的代码。

public async Task<Certificate> GetCertificate()
{
          await CertificateEnrollmentManager.ImportPfxDataAsync(Certificate, "", ExportOption.Exportable, KeyProtectionLevel.NoConsent, InstallOptions.None, SubscriptionID);
          CertificateQuery query = new CertificateQuery();
          query.FriendlyName = SubscriptionID;
          var c = await CertificateStores.FindAllAsync(query);
          return c[0];
}

public async Task<HttpResponseMessage> SendRequest(string url,string version)
{
        HttpResponseMessage response = null;
        try
        {
            HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter();
            filter.ClientCertificate = await GetCertificate();
            HttpClient client = new HttpClient(filter);
            HttpRequestMessage request = new HttpRequestMessage();
            request.RequestUri = new Uri(url);
            request.Headers.Add("x-ms-version", version);
            response = await client.SendRequestAsync(request, 0);
            return response;
        }
        catch(Exception e)
        {
            var status=Windows.Web.WebError.GetStatus(e.HResult);
            if (status == WebErrorStatus.CannotConnect)
                throw new Exception("Cannot connect to internet. Check your connection.");
            else if (status == WebErrorStatus.Disconnected)
                throw new Exception("Connection was disconnected.");
            else if (status == WebErrorStatus.ServiceUnavailable)
                throw new Exception("Server was unavailable");
            else if (status == WebErrorStatus.ConnectionReset)
                throw new Exception("Connection was reset.");
            else if (status == WebErrorStatus.BadGateway)
                throw new Exception("Bad gateway.");
            else if (status == WebErrorStatus.InternalServerError)
                throw new Exception("Internal server error occurred");
            else if (status == WebErrorStatus.HostNameNotResolved)
                throw new Exception("Check your network connection. Host name could not be resolved.");
        }
        return response;

 }

Windows Phone操作系统对应用程序的证书有限制吗?


当我尝试更改针对同一域的请求的客户端证书时,我遇到了类似的问题。我猜测您调用的管理终结点是相同的域。我认为这与底层库的状态有关,并以某种方式“缓存”客户端证书。然而,我没有找到任何关于这个问题或解决方案的提及。 - ameer
在Windows 7.5中,我记得我不能使用自签名证书。你是否需要在存储中安装自己的根CA? - Fabio Salvalai
1个回答

1

虽然没有直接回答如何处理您的证书问题,但我建议您使用更好的解决方法。

使用OAuth授权和Azure AD身份验证的Bearer令牌,而不是证书,来访问服务API。

因此,您只需使用ADAL从Azure AD获取一个令牌,而不是管理多个证书。您收到的单个令牌将适用于用户可以访问的所有订阅。

您可以在这里阅读有关使用Azure AD进行身份验证的服务管理API调用的更多信息authenticating service management API calls with Azure AD here

您还可以在这里了解有关在Windows Phone应用程序中使用ADAL的更多信息more about using ADAL with Windows Phone app here

您授予本机客户端应用程序访问Azure服务管理API的权限:

enter image description here


刚才给我点了踩的人请注意:服务证书即将被淘汰。使用证书的Azure服务管理API功能有限。你最好更新你的管理应用程序,让它们能够使用ADAL/MSAL和OAuth,并使用Azure资源管理器,而不是简单地点踩... - astaykov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接