如何使用模拟用户更新数据库 [英] how to update DataBase using impersonated user

查看:57
本文介绍了如何使用模拟用户更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个需要与数据库进行交互并根据需要对其进行更新的应用程序.

I have an application which need to interact with database and update it as required.

下面我有2个过程

AuthencateBufferLogin()-用于模拟用户(即与其他用户一起工作,执行某些任务

AuthencateBufferLogin() - Used to impersonate the user (i.e., to work with other user, to perform some task

QueryDataBase()-用于查询数据库

QueryDataBase() - Used to Query database

我可以使用此Buffer登录名(按目的地)来复制文件并根据需要创建文件.

i could able to copy files and create files as desired by using this Buffer login which has permissions in the destinated.

注意:出于安全原因,这些文件夹中其他用户的权限已删除

代码在这里...

 

Public Class BufferLogin

    Public Enum LogonType As Integer
        'This logon type is intended for users who will be interactively using the computer, 
        'such as a user being logged on
        'by a terminal server, remote shell, or similar process.
        'This logon type has the additional expense of caching logon information 
        'for disconnected operations;
        'therefore, it is inappropriate for some client/server applications,
        'such as a mail server.
        LOGON32_LOGON_INTERACTIVE = 2

        'This logon type is intended for high performance servers to authenticate plaintext passwords.
        'The LogonUser function does not cache credentials for this logon type.
        LOGON32_LOGON_NETWORK = 3

        'This logon type is intended for batch servers, where processes may be executing on behalf of a user without
        'their direct intervention. This type is also for higher performance servers that process many plaintext
        'authentication attempts at a time, such as mail or Web servers.
        'The LogonUser function does not cache credentials for this logon type.
        LOGON32_LOGON_BATCH = 4

        'Indicates a service-type logon. The account provided must have the service privilege enabled.
        '
        LOGON32_LOGON_SERVICE = 5

        'This logon type is for GINA DLLs that log on users who will be interactively using the computer.
        'This logon type can generate a unique audit record that shows when the workstation was unlocked.
        LOGON32_LOGON_UNLOCK = 7

        'This logon type preserves the name and password in the authentication package, which allows the server to make
        'connections to other network servers while impersonating the client. A server can accept plaintext credentials
        'from a client, call LogonUser, verify that the user can access the system across the network, and still
        'communicate with other servers.
        'NOTE: Windows NT:  This value is not supported.
        LOGON32_LOGON_NETWORK_CLEARTEXT = 8

        'This logon type allows the caller to clone its current token and specify new credentials for outbound connections.
        'The new logon session has the same local identifier but uses different credentials for other network connections.
        'NOTE: This logon type is supported only by the LOGON32_PROVIDER_WINNT50 logon provider.
        'NOTE: Windows NT:  This value is not supported.
        LOGON32_LOGON_NEW_CREDENTIALS = 9
    End Enum

    Public Enum LogonProvider As Integer
        'Use the standard logon provider for the system.
        'The default security provider is negotiate, unless you pass NULL for the domain name and the user name
        'is not in UPN format. In this case, the default provider is NTLM.
        'NOTE: Windows 2000/NT:   The default security provider is NTLM.
        LOGON32_PROVIDER_DEFAULT = 0
    End Enum


    Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As String, _
        ByVal lpszDomain As String, ByVal lpszPassword As String, ByVal dwLogonType As LogonType, _
        ByVal dwLogonProvider As LogonProvider, ByRef phToken As IntPtr) As Integer
    Declare Function ImpersonateLoggedOnUser Lib "advapi32.dll" (ByVal hToken As Integer) As Integer
    Declare Function RevertToSelf Lib "advapi32.dll" () As Integer

    Const LOGON32_PROVIDER_DEFAULT = 0
    Const LOGON32_LOGON_INTERACTIVE = 2
    Const LOGON32_LOGON_NETWORK = 3
    Const LOGON32_LOGON_BATCH = 4
    Const LOGON32_LOGON_SERVICE = 5
    Const LOGON32_LOGON_UNLOCK = 7

    Public BuffLoginFailMsg As String

    Public Function AuthenticateBufferLogin() As Boolean

        Dim nToken = 0

        If LogonUser("xyz", "abc.com", "xyz", LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, nToken) = 0 Then

            'MsgBox("login failed")
            BufferLoginStatus = False
            BuffLoginFailMsg = "Login Failed"

        Else

            'MsgBox("Login success")

            If ImpersonateLoggedOnUser(nToken) = 0 Then

                'MsgBox("Failed to authenticate. Unable to continue")
                BufferLoginStatus = False
                BuffLoginFailMsg = "Failed to authenticate. Unable to continue"

            Else

                'MsgBox("authentication success")
                BufferLoginStatus = True

            End If

        End If

        Return BufferLoginStatus

    End Function

    Public Sub QueryDatabase(ByVal QueryDataBase As String, _
                                     ByVal table As String, _
                                     ByVal SQL As String, _
                                     ByRef da As OleDbDataAdapter, _
                                     ByRef ds As DataSet)

        'Query DataBase based on SQL argument received
        'Return DataAdapter and DataSet

        If AuthenticateBufferLogin() Then

            Try

                ds.Clear()

                dbSource = "Data Source = " & DBPath & QueryDataBase

                conn.ConnectionString = dbProvider & dbSource & dbPassword

                My.Computer.FileSystem.WriteAllText(DBPath & "text.txt", "it will success", True)

                conn.Open()          'Here i get Unspecified Error

                da = New OleDbDataAdapter(SQL, conn)
                da.Fill(ds, table)

                conn.Close()

            Catch ex As Exception

                conn.Close()
                MsgBox(ex.Message, vbOKOnly, "Get Details")

            End Try

        Else

            MsgBox(BuffLoginFailMsg, vbOKOnly, "Get Details Authentication")

        End If

        RevertToSelf()  'Control back to currrent user

        Return

    End Sub
End Class

推荐答案

这是关于DB2数据库还是Informix?

Is this about a DB2 database or about a Informix?

 


这篇关于如何使用模拟用户更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆