[英]403 Error on Azure API Only After Deploying to IIS
I think I have read every single thing on the internet about this (bold statement I know) but I can't work it out... 我想我已经读过互联网上的每件事(我知道这是粗体的说法),但我无法解决...
I have a very simple webpage that gets the status VMs on Azure, which works fine on my machine. 我有一个非常简单的网页,可以在Azure上获取状态VM,在我的计算机上可以正常运行。 I created a Cert on my local machine with makecert and debug runs fine.
我使用makecert在本地计算机上创建了一个证书,并且调试运行良好。 After deploying it to another server on IIS all I get is 403 errors.
将其部署到IIS上的另一台服务器后,我得到的是403错误。
Things I tried: 我尝试过的事情:
I changed my get cert code to more resemble an answer from a similar question, but finding the cert doesn't appear to be the issue.. if I remove the cert created on the server, I get a different error. 我更改了获取证书代码,使其更类似于类似问题的答案,但是发现该证书似乎不是问题所在。如果我删除了在服务器上创建的证书,则会收到另一个错误。
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;
Ref: how to connect to azure (management) rest api via C# in IIS 参考: 如何通过IIS中的C#连接到Azure(管理)Rest API
Code to create HttpClient: 创建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;
}
Retrieve VMs Code: 检索虚拟机代码:
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);
}
}
}
This code is currently about 95% copy and paste from here 此代码目前大约有95%从此处复制粘贴
The resolution for me in this case was to create a new Publishsettings file via powershell and import that on the server via powershell. 在这种情况下,我的解决方法是通过powershell创建一个新的Publishsettings文件,然后通过powershell将其导入服务器。 Then use the thumbprint from that in code.
然后使用代码中的指纹。 Making a cert on the server and uploading to Azure still doesn't work for whatever reason...
无论出于何种原因,在服务器上制作证书并上传到Azure仍然不起作用...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.