VB.Net 应用程序通过 MFA 连接到 SQL Azure 实例

问题描述 投票:0回答:1

我有一个 VB.NET 应用程序,它有 2 个例程用于在 SQL 中打开数据库,这些例程都工作正常,直到我们使用 MFA 将数据库迁移到 SQL Azure 实例。

第一个序列与 MFA 配合良好,并经过身份验证以显示可供选择的数据库列表。但是,当我从应用程序中的选择中选择数据库时,我无法再次收到 MFA 提示进行身份验证

任何帮助表示赞赏

Private Sub ImageComboBoxDatabase_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ImageComboBoxDatabase.SelectedIndexChanged

        
        Dim listDataBases As New List(Of String)()
        Dim connectString As String
        Dim selectSQL As String

        Server = ImageComboBoxDatabase.EditValue.ToString
        ConnectString = "Data Source=" & server & " ; Authentication=Active Directory Interactive; Encrypt=True; Initial Catalog=master; User Id=" & UserNameVariable


        Using con As New SqlConnection(connectString)
            con.Open()
            selectSQL = "Select name from sys.databases where name Like 'cw_%';"
            Dim com As SqlCommand = New SqlCommand(selectSQL, con)
            Dim dr As SqlDataReader = com.ExecuteReader()
            While (dr.Read())
                listDataBases.Add(dr(0).ToString())
            End While
            GridLookUpEditDropDown.Properties.DataSource = listDataBases
        End Using
    End Sub
Private Sub SimpleButtonProjectSwitch_Click(sender As Object, e As EventArgs) Handles SimpleButtonProjectSwitch.Click
          
          
            CnString = "Data Source=" & server & " ; Authentication=Active Directory Interactive; Encrypt=True; Initial Catalog=master; User Id=" & UserNameVariable
            
            'Treelist
            TreeListTableAdapter.Connection.ConnectionString = Cnstring
    End Sub

如果我使用 SQL 登录用户名而不是我需要的经过身份验证的用户,我可以让它工作

sql vb.net azure instance multi-factor-authentication
1个回答
0
投票

在 TreeListTableAdapter 需要一些数据之前,您的应用程序不会尝试连接到数据库。

从你的例子中我看不出你在哪里使用它。

作为一个想法,您可能想要更改 SqlConnection 对象的范围。就目前情况而言,由于您实现了

using
,因此您的 SqlConnection 在查询后将被释放。您可以将其设为全局变量并重用它,以允许重复使用凭据的一次性身份验证。

Private connection As SqlConnection ' Declare a class-level variable to store the connection

Private Sub ImageComboBoxDatabase_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ImageComboBoxDatabase.SelectedIndexChanged
    Dim listDataBases As New List(Of String)()
    Dim server As String = ImageComboBoxDatabase.EditValue.ToString()
    Dim connectString As String = "Data Source=" & server & "; Authentication=Active Directory Interactive; Encrypt=True; Initial Catalog=master; User Id=" & UserNameVariable

    connection = New SqlConnection(connectString) ' Store the connection
    connection.Open()

    Dim selectSQL As String = "SELECT name FROM sys.databases WHERE name LIKE 'cw_%';"

    Using com As New SqlCommand(selectSQL, connection)
        Using dr As SqlDataReader = com.ExecuteReader()
            While dr.Read()
                listDataBases.Add(dr(0).ToString())
            End While
        End Using
    End Using

    GridLookUpEditDropDown.Properties.DataSource = listDataBases
End Sub

Private Sub SimpleButtonProjectSwitch_Click(sender As Object, e As EventArgs) Handles SimpleButtonProjectSwitch.Click
    Dim server As String = ImageComboBoxDatabase.EditValue.ToString()

    Try
        TreeListTableAdapter.Connection = connection ' Reuse the stored connection

        ' You can also open the connection if it's closed, but in this case, it's already open
        ' If TreeListTableAdapter.Connection.State = ConnectionState.Closed Then
        '     TreeListTableAdapter.Connection.Open()
        ' End If

' Add your logic here to populate data to the form controls

    Catch ex As Exception
        MessageBox.Show("Error switching database: " & ex.Message)
    End Try
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.