gpt4 book ai didi

c# - 添加 https 绑定(bind)时 Microsoft.Web.Administration 中的 NullReferenceException

转载 作者:太空狗 更新时间:2023-10-29 20:17:50 25 4
gpt4 key购买 nike

我正在尝试以编程方式将绑定(bind)添加到我的默认网站,但是我一直在 Microsoft.Web.Administration dll 中收到空引用异常。最初我想在绑定(bind)的同时分配一个证书。我可以用这个查询我想要的证书:

var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite);

var certificate = store.Certificates.Find(X509FindType.FindByIssuerName,
"TEST_SELF_SIGNED", true)
.OfType<X509Certificate>().FirstOrDefault();

这正确地给了我想要的证书,它是非空的并且有我期望的信息。

Site site = GetSite("Default Web Site");
var binding = site.Bindings.Add("*:443", certificate.GetCertHash(), "https");

鉴于我的变量或示例代码中的任何其他项目都不是空值(包括返回 20 字节数组的 GetCertHash),我很困惑为什么我在这里得到空值。我什至尝试了以下重载:

site.Bindings.Add("*:443", "https");

我仍然得到相同的空引用堆栈:

System.NullReferenceException was unhandled  Message=Object reference not set to an instance of an object.  Source=Microsoft.Web.Administration  StackTrace:       at Microsoft.Web.Administration.Configuration.SetDirty()       at Microsoft.Web.Administration.ConfigurationElement.SetDirty()       at Microsoft.Web.Administration.ConfigurationElement.SetAttributeValue(String attributeName, Object value)       at Microsoft.Web.Administration.Binding.SetBindingProperty(String attributeName, String value)       at Microsoft.Web.Administration.BindingCollection.Add(String bindingInformation, Byte[] certificateHash, String certificateStoreName)       at TestApp.Program.Main(String[] args) in C:\Projects\Cube\trunk\src\AutoUpdate\TestApp\Program.cs:line 33       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)       at System.Threading.ThreadHelper.ThreadStart()  InnerException: 

Here is a full test-app that demonstartes the issue, along with the selfssl command line arguments I used to generate the sample certificate:

selfssl.exe /T /N:CN=TEST_SELF_SIGNED /K:512 /V:9999 /Q

class Program
{
static void Main(string[] args)
{

using (ServerManager manager = new ServerManager())
{
var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite);

var certificate = store.Certificates.Find(X509FindType.FindByIssuerName, "TEST_SELF_SIGNED", true).OfType<X509Certificate>().FirstOrDefault();

Site site = GetSite("Default Web Site");
site.Bindings.Add("*:443", certificate.GetCertHash(), store.Name);

store.Close();

manager.CommitChanges();
}
}

public static Site GetSite(string siteName)
{
using (var serverManager = new ServerManager())
{
return serverManager.Sites.Where(p => p.Name.ToLower() == siteName.ToLower()).FirstOrDefault();
}
}
}

为了说明我的基本情况,安装了 Iis 并手动分配证书工作得很好。

最佳答案

因此,我通过反编译 Microsoft.Web.Administration dll 并查看堆栈找到了答案。事实证明,如果您获得一个带有辅助函数的站点,它不会在该站点上设置内部 ServerManager 属性。

导致问题的 dll 函数是 Microsoft.Web.Administration::Configuration 中的这个函数

internal void SetDirty()
{
if (this._hasBeenCommitted || this._configurationManager.Owner.ReadOnly)
throw new InvalidOperationException(Resources.ObjectHasBeenCommited);
this._isDirty = true;
}

此处唯一可能为空的是 _configurationManager_configurationManager.Owner。我检查了 Owner 是什么,它是一个 ServerManager,它告诉我我应该从服务器管理器的使用 block 中查询 Site .一旦我这样做了,空引用就消失了,一切正常。不幸的是,他们没有检查空值,但可能假设没有服务器管理器上下文,没有人会在站点对象上执行操作。

无论如何,这是更新后的代码:

class Program
{
static void Main(string[] args)
{

using (var serverManager = new ServerManager())
{
var selfSignedCnName = "TEST_SELF_SIGNED";
var websiteName = "Default Web Site";

var site = serverManager.Sites.Where(p => p.Name.ToLower() == websiteName.ToLower()).FirstOrDefault();
var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite);

var certificate = store.Certificates.Find(X509FindType.FindByIssuerName, selfSignedCnName, true).OfType<X509Certificate>().FirstOrDefault();

site.Bindings.Add("*:443:", certificate.GetCertHash(), store.Name);

store.Close();

serverManager.CommitChanges();
}
}

}

从我最初的帖子中也可以清楚地看出,将整个代码块包装在服务器管理器中没有任何意义,它们没有级联。您必须从它来自的服务器管理器对该站点进行操作。

关于c# - 添加 https 绑定(bind)时 Microsoft.Web.Administration 中的 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10384996/

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