Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 从Delphi应用程序使用Windows身份验证连接到Sql Server时以编程方式更改用户名_Sql Server_Delphi_Windows Authentication - Fatal编程技术网

Sql server 从Delphi应用程序使用Windows身份验证连接到Sql Server时以编程方式更改用户名

Sql server 从Delphi应用程序使用Windows身份验证连接到Sql Server时以编程方式更改用户名,sql-server,delphi,windows-authentication,Sql Server,Delphi,Windows Authentication,我有一个使用Windows身份验证的Sql Server 我想从Delphi应用程序连接到服务器 默认情况下,SQL Server将采用启动连接进程的用户的凭据 这意味着要更改登录,我当前有两个选项: 根据需要注销并登录 用户,然后运行我的应用程序 从命令启动程序 使用RUNAS命令的行 我想让用户从应用程序中提供凭据,并以该用户的身份登录。通过操纵ConnectionString或通过编程更改当前进程的用户,这是可能的吗 我找到的最接近的是,它告诉我如何在特定凭据下启动另一个进程。我可以使用这

我有一个使用Windows身份验证的Sql Server

我想从Delphi应用程序连接到服务器

默认情况下,SQL Server将采用启动连接进程的用户的凭据

这意味着要更改登录,我当前有两个选项:

  • 根据需要注销并登录 用户,然后运行我的应用程序

  • 从命令启动程序 使用RUNAS命令的行

  • 我想让用户从应用程序中提供凭据,并以该用户的身份登录。通过操纵ConnectionString或通过编程更改当前进程的用户,这是可能的吗

    我找到的最接近的是,它告诉我如何在特定凭据下启动另一个进程。我可以使用这种技术创建一个“启动器”程序,在从用户收集凭据后启动连接过程,但我真的想要更干净的东西

    我在这个项目中使用Delphi2010


    谢谢

    我想和的组合会对你有帮助。这将更改运行当前进程的用户帐户。正如gbn所提到的,在更改登录凭据之前,您可能必须断开任何活动连接。

    使用Scott W建议的方法,此代码对我有效。其中一些可能需要根据您的特定网络环境进行调整

    procedure ChangeLoggedInUser(username, password, domain: string);
    var
      creds: Cardinal;
    begin
      try
        if LogonUser(PChar(username)
            ,PChar(domain)
            ,PChar(password)
            ,LOGON32_LOGON_NETWORK
            ,LOGON32_PROVIDER_DEFAULT
            ,creds
          )
        then begin
          ImpersonateLoggedOnUser(creds);
        end
        else begin
          RaiseLastOSError;
        end;
      finally
        //wipe the memory for security
        FillChar(username,SizeOf(username),#0);
        FillChar(password,SizeOf(username),#0);
        FillChar(domain,SizeOf(username),#0);
      end;  //try-finally
    end;
    
    可以这样调用此代码:

    ...
    //at this point i am logged in as whoever is logged into the local machine
    DoSomethingMundane;
    
    //change credentials of the current thread
    ChangeLoggedInUser('importantuser','secretpassword','mydomain');
    
    //now my process will be logged in as "importantuser"
    DoSomethingThatRequiresCreds;
    
    //go back to normal
    ReverToSelf;
    
    //now my process is back to normal
    DoSomethingMundane;
    

    也许比我的答案更好。我关注的是DB端,而不是代码。模拟只针对当前线程(这是一个优势),如果需要处理多个用户/连接,可以在单独的线程中运行它们。它不使用“用户名”。它使用进程安全凭据。每个正在运行的进程都附加了安全凭据。ScottW给出了正确的答案-您必须更改进程/线程安全凭据。