通过证书认证使用REST API 返回ForbiddenError

weixin_38053088 2016-05-04 11:10:09
通过如下的方式获取证书,并通过Microsoft.WindowsAzure.Management.Sql.SqlManagementClient获取数据库信息可以成功:
public static X509Certificate2 GetStoreCertificate(string thumbprint)
{

var m_X509CertificateThumbprint = Regex.Replace(thumbprint, @"[^\u0000-\u007F]", string.Empty);
List<StoreLocation> locations = new List<StoreLocation>
{
StoreLocation.CurrentUser,
StoreLocation.LocalMachine
};

foreach (var location in locations)
{
X509Store store = new X509Store("My", location);
try
{
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
X509Certificate2Collection certificates = store.Certificates.Find(
X509FindType.FindByThumbprint, m_X509CertificateThumbprint, false);
if (certificates.Count == 1)
{
return certificates[0];
}
}
finally
{
store.Close();
}
}
throw new ArgumentException(string.Format(
"A Certificate with Thumbprint '{0}' could not be located.",
thumbprint));
}
var cert = GetStoreCertificate(certThumbprint);
SubscriptionCloudCredentials credential = new CertificateCloudCredentials(subscriptionID, cert);

var client = new Microsoft.WindowsAzure.Management.Sql.SqlManagementClient(credential, new Uri(ServiceManagementUrl));

return client.Databases.List(server);
但同样使用该证书,通过REST接口方式,就返回如下错误:
var cert = GetStoreCertificate(certThumbprint);
var credential = new Microsoft.Rest.CertificateCredentials(cert);

var client = new Microsoft.Azure.Management.ResourceManager.ResourceManagementClient(new Uri(ServiceManagementUrl), credential);
client.SubscriptionId = subscriptionID;

var t= client.ResourceGroups.GetWithHttpMessagesAsync("resourcegroup");
t.Wait();
var result = t.Result;


"<Error xmlns=\"http://schemas.microsoft.com/windowsazure\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><Code>ForbiddenError</Code><Message>The server failed to authenticate the request. Verify that
the certificate is valid and is associated with this subscription.</Message></Error>"

请问如何解决?
...全文
19 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复

476

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧