-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

http://www.developerfusion.com/将您的 C# 代码转换为 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 回答
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 回答