Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.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 在Visual Studio团队服务中以windows用户身份运行_Sql Server_Azure Devops_Windows Authentication_Azure Pipelines - Fatal编程技术网

Sql server 在Visual Studio团队服务中以windows用户身份运行

Sql server 在Visual Studio团队服务中以windows用户身份运行,sql-server,azure-devops,windows-authentication,azure-pipelines,Sql Server,Azure Devops,Windows Authentication,Azure Pipelines,我需要在TeamServices中构建一个VisualStudio项目,然后运行一些测试。测试进行RESTAPI调用,然后通过直接访问数据库验证RESTAPI调用是否有效。该数据库是Microsoft SQL Server数据库,仅允许Windows身份验证用户访问。它在内部服务器上运行。因此,当我在本地计算机上运行构建时,它可以正常工作,但在团队服务中,测试失败,因为它们无法访问数据库。我想知道是否有可能以用户身份运行测试,或者以某种方式解决这个问题 欢迎任何建议,但请注意,我绝对需要能够直接

我需要在TeamServices中构建一个VisualStudio项目,然后运行一些测试。测试进行RESTAPI调用,然后通过直接访问数据库验证RESTAPI调用是否有效。该数据库是Microsoft SQL Server数据库,仅允许Windows身份验证用户访问。它在内部服务器上运行。因此,当我在本地计算机上运行构建时,它可以正常工作,但在团队服务中,测试失败,因为它们无法访问数据库。我想知道是否有可能以用户身份运行测试,或者以某种方式解决这个问题


欢迎任何建议,但请注意,我绝对需要能够直接访问数据库以验证API。

通过VSTS build构建项目,它使用VSTS代理帐户(可以在安装VSTS代理时指定)

首先,如果您正在使用vsts代理进行构建和测试,并且该计算机中的windows帐户可以连接到SQL Server并读取数据,则可以模拟用户从SQL Server访问数据

简单代码:

public static class Helper
{
    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool LogonUser(
        string lpszUsername,
        string lpszDomain,
        string lpszPassword,
        int dwLogonType,
        int dwLogonProvider,
        out IntPtr phToken);
}
--

}

其次,如果您使用的是Hosted Agent,唯一的方法是您需要使用SQL Server帐户来访问SQL Server数据库,还要确保您的SQL Server实例可以从Hosted Agent访问

IntPtr userToken = IntPtr.Zero;

        bool success = Helper.LogonUser(
          "[user name]",
          "[domain]",
          "[password]",
          2, //2
          0, //0
          out userToken);

        if (!success)
        {
            Assert.Fail("Logon user failed");

        }

        using (WindowsIdentity.Impersonate(userToken))
        {
          //TODO connect to your sql server database