C# 使用HttpClient的多个证书
我正在构建一个Windows Phone 8.1应用程序,允许Azure用户使用Azure服务管理API查看其订阅/服务。身份验证是使用管理证书完成的,证书附加到API的所有请求中。对于单个用户来说,它工作得很好。但当我尝试为多个订阅包含一个特性时,问题就出现了。我能够在证书存储中安装证书并检索它。但是当我向API发送请求时,问题就出现了。即使我附上了正确的证书,我还是收到了403禁止的错误。 这是我使用的代码C# 使用HttpClient的多个证书,c#,azure,ssl,windows-phone-8.1,C#,Azure,Ssl,Windows Phone 8.1,我正在构建一个Windows Phone 8.1应用程序,允许Azure用户使用Azure服务管理API查看其订阅/服务。身份验证是使用管理证书完成的,证书附加到API的所有请求中。对于单个用户来说,它工作得很好。但当我尝试为多个订阅包含一个特性时,问题就出现了。我能够在证书存储中安装证书并检索它。但是当我向API发送请求时,问题就出现了。即使我附上了正确的证书,我还是收到了403禁止的错误。 这是我使用的代码 public async Task<Certificate> GetCe
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;
}
公共异步任务GetCertificate()
{
等待CertificateRollmentManager.ImportPfxDataAsync(证书,”,ExportOption.Exportable,KeyProtectionLevel.NoConsent,InstallOptions.None,SubscriptionID);
CertificateQuery=新建CertificateQuery();
query.FriendlyName=SubscriptionID;
var c=等待CertificateStores.findalsync(查询);
返回c[0];
}
公共异步任务SendRequest(字符串url,字符串版本)
{
HttpResponseMessage响应=null;
尝试
{
HttpBaseProtocolFilter=新的HttpBaseProtocolFilter();
filter.ClientCertificate=等待GetCertificate();
HttpClient=新的HttpClient(过滤器);
HttpRequestMessage请求=新建HttpRequestMessage();
request.RequestUri=新Uri(url);
添加(“x-ms-version”,version);
response=wait client.SendRequestAsync(请求,0);
返回响应;
}
捕获(例外e)
{
var status=Windows.Web.WebError.GetStatus(e.HResult);
如果(状态==WebErrorStatus.CannotConnect)
抛出新异常(“无法连接到internet。请检查您的连接”);
else if(状态==WebErrorStatus.Disconnected)
抛出新异常(“连接已断开”);
else if(状态==WebErrorStatus.ServiceUnavailable)
抛出新异常(“服务器不可用”);
else if(状态==WebErrorStatus.ConnectionReset)
抛出新异常(“连接已重置”);
else if(状态==WebErrorStatus.BadGateway)
抛出新异常(“坏网关”);
else if(状态==WebErrorStatus.InternalServerError)
抛出新异常(“发生内部服务器错误”);
else if(状态==WebErrorStatus.HostNameNotResolved)
抛出新异常(“检查网络连接。无法解析主机名”);
}
返回响应;
}
Windows Phone操作系统对应用程序的证书有限制吗?虽然没有直接回答如何处理您的证书问题,但我建议您采取一种更有效的解决方法 将OAuth授权与承载令牌和Azure AD身份验证一起用于服务API,而不是证书 因此,您不需要管理多个证书,只需使用ADAL从Azure AD获取令牌。您收到的单个令牌将对用户有权访问的所有订阅有效 你可以阅读更多关于 你可以学习 您授予本机客户端对Azure服务管理API的应用程序访问权限:
在尝试更改针对同一域的请求的客户端证书时,我遇到了类似的情况。我猜您要调用的管理端点是同一个域。我认为这与底层库的状态和“缓存”客户端证书有关。我还没有发现任何关于这个问题或解决方案的提及。在Windows7中。我记得我不能使用自签名证书。您是否必须在应用商店中安装自己的根CA?无论谁刚刚否决了服务证书,都将退役。与证书一起使用的Azure服务管理API功能有限。您最好更新您的管理应用程序以与ADAL/MSAL和OAuth一起使用,并使用Azure资源管理器而不是向下投票。。。