【发布时间】: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