c# - 使用 HttpClient 的多个证书

标签 c# azure ssl windows-phone-8.1

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

最佳答案

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

对服务 API 使用具有不记名 token 的 OAuth 授权和 Azure AD 身份验证,而不是证书。

因此,您无需管理多个证书,只需使用 ADAL 从 Azure AD 获取 token 即可。您收到的单个 token 将对用户有权访问的所有订阅有效。

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

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

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

enter image description here

关于c# - 使用 HttpClient 的多个证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27501095/

相关文章:

c# - SQLite 锁定文件,即使连接已关闭

c# - Sitecore azure 无法启动

cakephp - OSX 本地 SSL 不受尊重?

django - 在 Visual Studio Online 上构建 Django

Magento 在 Gmail 上配置电子邮件

asp.net - 让 https 在 asp.net MVC4 应用程序中本地工作

c# - 关于 SetSocketOption 和 MulticastOption 的问题

c# - 有没有办法捕获脚本在使用 SqlCommand 时生成的数据库消息?

c# - 如何从表达式树中获取参数名称?

c# - 如何使用对 Azure 表存储的单个查询检索多种类型的实体?