-2

次のコードをVB.netに変換しようとしています

private void Application_Start(object sender, EventArgs e)
    {
        var defaultTableData = new DefaultTableData();
        defaultTableData.CheckAndUpdate();

        if (ConfigurationManager.AppSettings["RSAConfigSwitch"].ToString().ToUpper() == "ON")
        {
            FederatedAuthentication.ServiceConfigurationCreated += new EventHandler<ServiceConfigurationCreatedEventArgs>(FederatedAuthentication_ServiceConfigurationCreated);

        }

    }

    void FederatedAuthentication_ServiceConfigurationCreated(object sender, Microsoft.IdentityModel.Web.Configuration.ServiceConfigurationCreatedEventArgs e)
    {
        String certName = ConfigurationManager.AppSettings["CertificateName"].ToString(); // read from web.config
        System.Security.Cryptography.X509Certificates.X509Store store = new System.Security.Cryptography.X509Certificates.X509Store(System.Security.Cryptography.X509Certificates.StoreName.My, System.Security.Cryptography.X509Certificates.StoreLocation.LocalMachine);
        store.Open(OpenFlags.ReadOnly);
        System.Security.Cryptography.X509Certificates.X509Certificate2Collection col = store.Certificates.Find(System.Security.Cryptography.X509Certificates.X509FindType.FindBySubjectName, certName, true);
        var cookieProtectionCertificate = col[0];

        e.ServiceConfiguration.SecurityTokenHandlers.AddOrReplace(
            new SessionSecurityTokenHandler(new System.Collections.ObjectModel.ReadOnlyCollection<CookieTransform>(
                new List<CookieTransform> 
                    { 
                        new DeflateCookieTransform(), 
                        new RsaEncryptionCookieTransform(cookieProtectionCertificate), 
                        new RsaSignatureCookieTransform(cookieProtectionCertificate) 
                    })
            ));
    }

変換された VB コード

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) 
        ' Fires when the application is started 
        FederatedAuthentication.ServiceConfigurationCreated += New EventHandler(Of ServiceConfigurationCreatedEventArgs)(FederatedAuthentication_ServiceConfigurationCreated)





    End Sub
Private Sub FederatedAuthentication_ServiceConfigurationCreated(ByVal sender As Object, ByVal e As Microsoft.IdentityModel.Web.Configuration.ServiceConfigurationCreatedEventArgs)  

        Try 
            Dim appCertificateName As String = System.Configuration.ConfigurationManager.AppSettings("adfsCertName") 
            If String.IsNullOrEmpty(appCertificateName) Then 
                Throw New Exception("ADFS_CERTIFICATE in config is empty") 
            End If 
            Dim store As X509Store = New X509Store(StoreName.My, StoreLocation.LocalMachine) 
            store.Open(OpenFlags.ReadOnly) 
            Dim col As X509Certificate2Collection = store.Certificates.Find(X509FindType.FindBySubjectName, appCertificateName, True)

            Dim cookieProtectionCertificate As X509Certificate2 = col(0) 
           e.ServiceConfiguration.SecurityTokenHandlers.AddOrReplace(New SessionSecurityTokenHandler(New System.Collections.ObjectModel.ReadOnlyCollection(Of CookieTransform)(New List(Of CookieTransform)() With { _

        New DeflateCookieTransform(), _ 
        New RsaEncryptionCookieTransform(cookieProtectionCertificate), _ 
        New RsaSignatureCookieTransform(cookieProtectionCertificate) _ 
})))                                                                          


        Catch ex As Exception 
            Throw ex 
        End Try 
    End Sub

しかし、次のエラー エラー 103 'Public Shared Event ServiceConfigurationCreated(sender As Object, e As Microsoft.IdentityModel.Web.Configuration.ServiceConfigurationCreatedEventArgs)' はイベントであり、直接呼び出すことはできません。「RaiseEvent」ステートメントを使用してイベントを発生させます。

誰かがコードの変換を手伝ってくれますか?

4

3 に答える 3

0

変換でいくつかの問題があります-これを試してください:

Private Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    Dim defaultTableData = New DefaultTableData()
    defaultTableData.CheckAndUpdate()

    If ConfigurationManager.AppSettings("RSAConfigSwitch").ToString().ToUpper() = "ON" Then
        AddHandler FederatedAuthentication.ServiceConfigurationCreated, AddressOf FederatedAuthentication_ServiceConfigurationCreated
    End If
End Sub

Private Sub FederatedAuthentication_ServiceConfigurationCreated(ByVal sender As Object, ByVal e As Microsoft.IdentityModel.Web.Configuration.ServiceConfigurationCreatedEventArgs)
    Dim certName As String = ConfigurationManager.AppSettings("CertificateName").ToString() ' read from web.config
    Dim store As New System.Security.Cryptography.X509Certificates.X509Store(System.Security.Cryptography.X509Certificates.StoreName.My, System.Security.Cryptography.X509Certificates.StoreLocation.LocalMachine)
    store.Open(OpenFlags.ReadOnly)
    Dim col As System.Security.Cryptography.X509Certificates.X509Certificate2Collection = store.Certificates.Find(System.Security.Cryptography.X509Certificates.X509FindType.FindBySubjectName, certName, True)
    Dim cookieProtectionCertificate = col(0)

    e.ServiceConfiguration.SecurityTokenHandlers.AddOrReplace(New SessionSecurityTokenHandler(New System.Collections.ObjectModel.ReadOnlyCollection(Of CookieTransform)(New List(Of CookieTransform) From {
        New DeflateCookieTransform(),
        New RsaEncryptionCookieTransform(cookieProtectionCertificate),
        New RsaSignatureCookieTransform(cookieProtectionCertificate)
    })))
End Sub
于 2012-10-05T14:51:14.070 に答える
0

C# コードをhttp://www.developerfusion.com/から VB.net に変換した後、そこから以下のコードを取得しました。

Private Sub Application_Start(sender As Object, e As EventArgs) Dim defaultTableData = New DefaultTableData() defaultTableData.CheckAndUpdate()

If ConfigurationManager.AppSettings("RSAConfigSwitch").ToString().ToUpper() = "ON" Then

    FederatedAuthentication.ServiceConfigurationCreated += New EventHandler(Of ServiceConfigurationCreatedEventArgs)(AddressOf FederatedAuthentication_ServiceConfigurationCreated)
End If

End Sub

Private Sub FederatedAuthentication_ServiceConfigurationCreated(sender As Object, e As Microsoft.IdentityModel.Web.Configuration.ServiceConfigurationCreatedEventArgs) Dim certName As [String] = ConfigurationManager.AppSettings("CertificateName").ToString() ' read from web.config Dim store As New System.Security.Cryptography.X509Certificates.X509Store(System.Security.Cryptography.X509Certificates.StoreName.My, System.Security.Cryptography.X509Certificates.StoreLocation.LocalMachine) store.Open(OpenFlags.[ReadOnly]) Dim col As System.Security.Cryptography.X509Certificates.X509Certificate2Collection = store.Certificates.Find(System.Security.Cryptography.X509Certificates.X509FindType.FindBySubjectName, certName, True) Dim cookieProtectionCertificate = col(0)

e.ServiceConfiguration.SecurityTokenHandlers.AddOrReplace(New SessionSecurityTokenHandler(New System.Collections.ObjectModel.ReadOnlyCollection(Of CookieTransform)(New List(Of CookieTransform)() From { _
    New DeflateCookieTransform(), _
    New RsaEncryptionCookieTransform(cookieProtectionCertificate), _
    New RsaSignatureCookieTransform(cookieProtectionCertificate) _
})))

End Sub

これが役立つことを願っています。

于 2012-10-05T13:47:37.680 に答える
0

コンパイラ エラーは、イベント ハンドラーに何か問題があることを示しています。VB は、ハンドラーを追加するための += 構文をサポートしていません。代わりに AddHandler/AddressOf キーワードを使用します。

   AddHandler  FederatedAuthentication.ServiceConfigurationCreated, AddressOf FederatedAuthentication_ServiceConfigurationCreated
于 2012-10-05T13:48:15.293 に答える