简体   繁体   English

从托管的 ASP.NET MVC 应用程序添加 https 绑定

[英]Adding https binding from ASP.NET MVC application hosted

I have an Application hosted in IIS website TestWebsite and i am using Microsoft.Web.Administration for IIS Automation.我在 IIS 网站TestWebsite中托管了一个应用程序,并且我正在使用 Microsoft.Web.Administration 进行 IIS 自动化。

I have to do following operations with bindings of the SAME website TestWebsite from TestWebsite我必须对来自TestWebsite的 SAME 网站TestWebsite的绑定进行以下操作

  1. Add HTTPS binding with SSL certificate to the website "TestWebsite" from same application (code to add binding will be in same "TestWebsite")将 HTTPS 与 SSL 证书的绑定从同一应用程序添加到网站“TestWebsite”(添加绑定的代码将在同一“TestWebsite”中)
  2. Remove the Binding.移除绑定。

I have done the following code and weird thing is that on localhost it is adding the https binding but even before manager.commitchange() .我已经完成了以下代码,奇怪的是它在 localhost 上添加了 https 绑定,但甚至在manager.commitchange()之前。 This line throw exception on local host, so i removed this line but on Windows Server its not adding the binding even after successfully running the code.此行在本地主机上引发异常,因此我删除了此行,但在 Windows 服务器上,即使在成功运行代码后也没有添加绑定。 (without commitchanges(), i hv no idea how its working on localhost without it) (没有commitchanges(),我不知道没有它如何在本地主机上工作)

I am using IIS 10 on localhost and IIS 8.5 on staging and IIS 8.0 On Production.我在 localhost 上使用 IIS 10,在暂存上使用 IIS 8.5,在生产上使用 IIS 8.0。

using (ServerManager iisManager = new ServerManager())
{
    var website = iisManager.Sites.Where(x => x.Name == "TestWebsite").FirstOrDefault();
    if (website != null)
    {
        var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
        store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite);
        var pfxPath = Server.MapPath(model.PfxPath);
        var certificate = new X509Certificate2(pfxPath, password, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
        store.Add(certificate);
        store.Close();
        var certHash = certificate.GetCertHash();

        string bindingInformation = string.Format("{0}:{1}:{2}", "*", "443", model.UserCustom);
        var binding = website.Bindings.Add(bindingInformation, certHash, store.Name);
        binding.Protocol = "https";
        store.Close();

        website.ApplicationDefaults.EnabledProtocols = "http,https";
        iisManager.CommitChanges();
    }
}

I receive following error on iisManager.CommitChanges() line我在iisManager.CommitChanges()行收到以下错误

A specified logon session does not exist.指定的登录 session 不存在。 It may already have been terminated.它可能已经被终止。 (Exception from HRESULT: 0x80070520) (来自 HRESULT 的异常:0x80070520)

Is there some permission related error?是否有一些权限相关的错误? What i am doing wrong in it?我做错了什么?

Your help will be appreciated您的帮助将不胜感激

Thank you:) Danial Malik谢谢:)丹尼尔·马利克

IIS would return 0x80070520 error when Adminsitrators group don't have permission to access C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys.当管理员组无权访问 C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys 时,IIS 将返回 0x80070520 错误。 Please try to grant full control permission for your administrators group.请尝试为您的管理员组授予完全控制权限。

https://www.cnblogs.com/ziye/p/9208353.html https://www.cnblogs.com/ziye/p/9208353.html

https://blogs.msdn.microsoft.com/asiatech/2010/08/12/got-error-0x80070520-when-binding-certificate-to-web-site-on-iis-7/ https://blogs.msdn.microsoft.com/asiatech/2010/08/12/got-error-0x80070520-when-binding-certificate-to-web-site-on-iis-7/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM