VB.NET控制台应用程序修改iisClientCertificateMappingAuthentication .config文件

时间:2011-12-20 19:49:01

标签: vb.net console

我制作了一个小的控制台应用程序来帮助我将客户端证书重新映射到我的网络服务器。但是,当我尝试运行应用程序时,它给出了一个错误:

CertMapping.exe中0x00920309处的未处理异常:0xC0000005:访问冲突读取位置0x00000000。

这是一个非常基本的控制台应用程序:

Imports Microsoft.Web.Administration

Module Module1
Sub main(ByVal cmdArgs() As String)
    Dim WebSite As String = Nothing
    Dim UserName As String = Nothing
    Dim Password As String = Nothing
    Dim Base64EncodedCertData As String = Nothing
    Dim switch As String, arg As String

    For Each Str As String In cmdArgs
        switch = Split(Str, ":").First
        arg = Split(Str, ":").Last
        Select Case switch
            Case "/Web"
                WebSite = arg
            Case "/User"
                UserName = arg
            Case "/Pwd"
                Password = arg
            Case "/Cert"
                If arg = "mySecretCode" Then
                    Base64EncodedCertData = "ServerCertGoesHere"
                Else
                    Base64EncodedCertData = arg
                End If
        End Select
    Next

    Using serverManager As New ServerManager
        Dim config As Configuration = serverManager.GetWebConfiguration(WebSite.ToString)

        Dim iisClientCertificateMappingAuthenticationSection As ConfigurationSection = config.GetSection("system.webServer/security/authentication/iisClientCertificateMappingAuthentication")
        iisClientCertificateMappingAuthenticationSection("enabled") = True
        iisClientCertificateMappingAuthenticationSection("oneToOneCertificateMappingsEnabled") = True

        Dim oneToOneMappingsCollection As ConfigurationElementCollection = iisClientCertificateMappingAuthenticationSection.GetCollection("oneToOneMappings")
        Dim addElement As ConfigurationElement = oneToOneMappingsCollection.CreateElement("add")
        addElement.SetMetadata("lockItem", True)
        addElement("enabled") = True
        addElement("userName") = UserName.ToString
        addElement("password") = Password.ToString
        addElement("certificate") = Base64EncodedCertData.ToString
        oneToOneMappingsCollection.Add(addElement)

        Dim accessSection As ConfigurationSection = config.GetSection("system.webServer/security/access", WebSite.ToString)
        accessSection("sslFlags") = "Ssl, SslNegotiateCert"

        serverManager.CommitChanges()
    End Using
End Sub

End Module

0 个答案:

没有答案