2016-04-08 14 views
0

私は今数日間問題を解決する方法を探していて、ほとんど何も見つかりませんでした。問題は次のとおりです。IIS 6のWebサイトにSSL証明書を添付する必要があります。httpsバインディングを設定できますが、証明書の拇印と証明書ストアを設定しようとするたびに、COM例外が発生します。ここに私のコードです。IISプログラムのサイトにSSL証明書を添付してください。

Private Const UnformattedMetabasePathForSiteProperties As String = "IIS://localhost/W3SVC/{0}" 
    Private metabasePath As String 
    Public Sub BindCertificateToSite(iisSiteId As Integer, ByVal aCertificate As X509Certificate2) Implements IIisSslHandler.BindCertificatetoSite 
     metabasePath = UnformattedMetabasePathForSiteProperties.FormatIt(iisSiteId) 
     Dim ipAddress As String = GetProperty(metabasePath, "ServerBindings") 
     Contract.Require(Not String.IsNullOrEmpty(ipAddress), New Exception("Failed to find the site's http binding and IP address. Can not bind certificate to site.")) 
     ipAddress = ipAddress.Split(":")(0) 
     SetProperty(metabasePath, "SecureBindings", ipAddress & ":443:", True) 
     SetProperty(metabasePath, "SSLStoreName", "MY", True) 
     SetBinaryProperty(metabasePath, "SSLCertHash", aCertificate.Thumbprint, True) 
    End Sub 

    Private Sub SetProperty(ByVal metabasePath As String, ByVal propertyName As String, ByVal newValue As Object, clearCurrentValue As Boolean) 
     Dim path As DirectoryEntry 
     path = New DirectoryEntry(metabasePath) 
     If clearCurrentValue Then path.Properties(propertyName).Clear() 
     path.Properties(propertyName).Add(newValue) 
     path.CommitChanges() 
    End Sub 

    Private Sub SetBinaryProperty(ByVal metabasePath As String, ByVal propertyName As String, ByVal newValue As Object, clearCurrentValue As Boolean) 
     Dim path As DirectoryEntry 
     path = New DirectoryEntry(metabasePath) 
     Dim propValues As PropertyValueCollection 
     propValues = path.Properties(propertyName) 
     If clearCurrentValue Then propValues.Clear() 
     propValues.Add(newValue) 
     path.CommitChanges() 
    End Sub 

私がいるように見えるスタックとネット上の他のサイトで、ここで他の質問を発見したSSLStoreNameとSSLCertHash

SSLStoreName exception: An exception of type 'System.Runtime.InteropServices.COMException' occurred in System.DirectoryServices.dll but was not handled in user code. Additional information: A specified logon session does not exist. It may already have been terminated. (Exception from HRESULT: 0x80070520) 

SSLCertHash Exception: An exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll but was not handled in user code. Additional information: Exception from HRESULT: 0x8000500C 

両方のための私はpropValues.Addコールでocurrsを取得する例外.Netフレームワークの脆弱性を指摘しているのは、IIS 6についてですが、ここではそうではないと考えています。だから私の質問:なぜこれは動作していないと私はそれを修正することができますか?

+0

証明書ハッシュ例外は、私が見つけたところから、無効なデータ型を指しています。しかし、私は考えることができるすべてのタイプを試しました。 MSDNはそのバイナリデータを言う。 –

答えて

0

だから、私はついにこれを働かせました。私は将来のスタッカーと私のためにこのコードを投稿しています。

Private Const UnformattedMetabasePathForSiteProperties As String = "IIS://localhost/W3SVC/{0}" 
Private metabasePath As String 
Public Sub BindCertificateToSite(iisSiteId As Integer, ByVal aCertificate As X509Certificate2) Implements IIisSslHandler.BindCertificatetoSite 
    metabasePath = UnformattedMetabasePathForSiteProperties.FormatIt(iisSiteId) 
    Dim ipAddress As String = GetProperty(metabasePath, "ServerBindings") 
    Contract.Require(Not String.IsNullOrEmpty(ipAddress), New Exception("Failed to find the site's http binding and IP address. Can not bind certificate to site.")) 
    ipAddress = ipAddress.Split(":")(0) 
    SetProperty(metabasePath, "SecureBindings", ipAddress & ":443:", True) 
    SetProperty(metabasePath, "SSLStoreName", "MY", True) 
    SetBinaryProperty(metabasePath, "SSLCertHash", aCertificate.GetCertHash(), True) 
End Sub 

Private Sub SetProperty(ByVal metabasePath As String, ByVal propertyName As String, ByVal newValue As Object, clearCurrentValue As Boolean) 
    Dim path As DirectoryEntry 
    path = New DirectoryEntry(metabasePath) 
    If clearCurrentValue Then path.Properties(propertyName).Clear() 
    path.Invoke("Put", propertyName, newValue) 
    path.CommitChanges() 
End Sub 

Private Sub SetBinaryProperty(ByVal metabasePath As String, ByVal propertyName As String, ByVal newValue As Object, clearCurrentValue As Boolean) 
    Dim path As DirectoryEntry 
    path = New DirectoryEntry(metabasePath) 
    Dim propValues As PropertyValueCollection 
    propValues = path.Properties(propertyName) 
    If clearCurrentValue Then propValues.Clear() 
    path.Invoke("Put", propertyName, newValue) 
    path.CommitChanges() 
End Sub 
+0

ちょうどメモ。昨年の夏にIIS 6が亡くなり、ほとんどのプログラマはIIS 7+用のMicrosoft.Web.Administration.dllベースのAPIに移行する必要があります。 –

+0

私はあなたに完全に同意します。しかし、私の要件では、IIS 6と7の両方のコード方法論を使用しています。つまり、同じソリューションを使用するだけでなく、同じタスクを完了するためにdllを使用することです。 –

関連する問題