我想我已经在互联网上阅读了关于这一点的所有内容(我知道的大胆声明),但我无法解决这个问题......
我有一个非常简单的网页,可以在Azure上获取状态虚拟机,这在我的机器上运行正常。我在本地机器上用makecert创建了一个Cert,调试运行正常。 在将其部署到IIS上的另一台服务器后,我得到的是403错误。
我尝试的事情:
我改变了我的获取证书代码更类似于类似问题的答案,但找到证书似乎不是问题..如果我删除在服务器上创建的证书,我会得到一个不同的错误
var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);
var certificate = store.Certificates.Cast<X509Certificate2>().SingleOrDefault(c => string.Equals(c.Thumbprint, thumbprint, StringComparison.OrdinalIgnoreCase)); // please replace CertificateThumbprint with original Thumbprint
return certificate;
参考:how to connect to azure (management) rest api via C# in IIS
创建HttpClient的代码:
WebRequestHandler handler = new WebRequestHandler();
String CertThumbprint = _certthumbprint;
X509Certificate2 managementCert = FindX509Certificate(CertThumbprint);
if (managementCert != null)
{
handler.ClientCertificates.Add(managementCert);
HttpClient httpClient = new HttpClient(handler);
httpClient.DefaultRequestHeaders.Add("x-ms-version", "2014-05-01");
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
return httpClient;
}
检索虚拟机代码:
String uri = String.Format("https://management.core.windows.net/{0}/services/hostedservices/{1}/deploymentslots/{2}", _subscriptionid, ServiceName, "Production");
XDocument vms = new XDocument();
vms.Add(new XElement("VirtualMachines"));
ApplyNamespace(vms.Root, ns);
try
{
HttpClient http = GetHttpClient();
Stream responseStream = await http.GetStreamAsync(uri);
if (responseStream != null)
{
XDocument xml = XDocument.Load(responseStream);
var roles = xml.Root.Descendants(ns + "RoleInstance");
foreach (XElement r in roles)
{
XElement svcNamee1 = new XElement("ServiceName", ServiceName);
ApplyNamespace(svcNamee1, ns);
r.Add(svcNamee1);
vms.Root.Add(r);
}
}
}
此代码目前约有95%来自here
的复制和粘贴答案 0 :(得分:0)
在这种情况下,我的解决方案是通过powershell创建一个新的Publishsettings文件,并通过PowerShell将其导入服务器。然后使用代码中的指纹。在服务器上制作证书并上传到Azure仍然无论出于何种原因都无法工作......