【问题标题】:Multiple certificates with HttpClient使用 HttpClient 的多个证书
【发布时间】:2015-02-14 13:19:52
【问题描述】:

我正在构建一个 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 操作系统对应用的证书有限制吗?

【问题讨论】:

  • 我在尝试更改针对同一域的请求的客户端证书时遇到了类似的情况。我猜你调用的管理端点是同一个域。我认为这与底层库状态和以某种方式“缓存”客户端证书有关。不过,我还没有找到任何关于这个问题或解决方案的提及。
  • 在 Windows 7. 5 中,我记得我不能使用自签名证书。您是否必须在商店中安装自己的根 CA?

标签: c# azure ssl windows-phone-8.1


【解决方案1】:

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

使用带有承载令牌的 OAuth 授权和服务 API 的 Azure AD 身份验证,而不是证书。

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

您可以在authenticating service management API calls with Azure AD here 上阅读更多内容。

你可以学习more about using ADAL with Windows Phone app here

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

【讨论】:

  • 刚刚投反对票的人 - 服务证书将被淘汰。使用证书的 Azure 服务管理 API 的功能有限。您最好更新管理应用程序以使用 ADAL/MSAL 和 OAuth,并使用 Azure 资源管理器而不是投票...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-04
  • 2011-02-21
  • 1970-01-01
相关资源
最近更新 更多