gpt4 book ai didi

c# - 使用 HttpClient 的多个证书

转载 作者:可可西里 更新时间:2023-11-01 07:48:32 27 4
gpt4 key购买 nike

我正在构建一个 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com