简体   繁体   English

模拟 SQL 连接字符串的用户 c# MVC

[英]Impersonate user for SQL connection strings c# MVC

I have couple of connection strings in web.config, ideally using those connection strings and the credentials specified (in web.config) I will have to open a connection and do an insert - for both the connections (lets says connection1 and connection 2).我有对夫妇在web.config中的连接字符串,最好使用这些连接字符串和指定(在web.config中)的凭据,我将不得不打开连接并执行插入 - 两者的连接(让说连接1和连接2) 。

Now connetcion1 and connection2 have different credentials.现在 connetcion1 和 connection2 具有不同的凭据。 I have tried using them in the connection string of web.config but it always says login failed for user.我已经web.config中的连接字符串中使用它们尝试,但它总是说用户登录失败。 Below are the connection strings.下面是连接字符串。

<connectionStrings>
<add name="connection_2" connectionString="Data Source=domain\servername;Initial Catalog=DBName;User ID=domain\xxxx;Password=abcgdd****;"  providerName="System.Data.SqlClient"/>

<add name="connection_3" connectionString="Data Source=domain\servername;Initial Catalog=DBName;User ID=domain\yyyy;Password=fgdd****;"  providerName="System.Data.SqlClient"/>
<connectionStrings> 

So after some googling understood that I have to use impersonation.所以经过一些谷歌上搜索了解,我必须使用模拟。

Using the below code for impersonation使用模拟下面的代码

using (new Impersonator("username","domain","pwd"))
{
// trying to open connection 1 from web.config but it says no such user. 

using (SqlConnection connection = new SqlConnection("Connection1FromConfig"))
                            {
                                cmd = new SqlCommand("select * from abc", connection);
                                connection.Open();
                                cmd.ExecuteNonQuery();
                                connection.Close();
                                
                            }
}

Connection used while impersonation is :

 <add name="Connection2" connectionString="Data Source=domain\server;Initial Catalog=DB;"  providerName="System.Data.SqlClient"/>

`````

Used the code from here for impersonation class - https://daoudisamir.com/impersonate-users-in-c/ 

You need to provide Trusted_Connection=yes;您需要提供Trusted_Connection=yes; in your connection string if you intend to use Windows Credentials with impersonate.如果您打算将 Windows 凭据与模拟一起使用,请在您的连接字符串中。

This is my connectionString that I used successfully,这是我成功使用的 connectionString,

string srvConnection = $"Server={serverName}; Trusted_Connection=yes; connection timeout=15";

I got the Impersonator class from here and was able to use it successfully.从这里得到了 Impersonator 类并且能够成功使用它。 SQL account I needed to use was different than the domain account being used with IIS / app pool.我需要使用的 SQL 帐户与用于 IIS/应用程序池的域帐户不同。

string srvConnection = $"Server={ListenerName}; Trusted_Connection=yes; connection timeout=15";
using (SqlConnection connection = new SqlConnection(srvConnection)) 
{
    using (new Impersonator(creds.Username.Split('\\').Last(), creds.Username.Split('\\').First(), creds.Password))
        connection.Open();

    // other code that needs to use the connection goes here.
    cmd = new SqlCommand("select * from abc", connection);
    cmd.ExecuteNonQuery();
}

You dont need to close the connection because of the using statement.由于using语句,您不需要关闭连接。 Since impersonation is only required to open the connection, you dont need to use it anywhere other than with connection.Open();因为模拟只需要打开连接,所以你不需要在除了connection.Open();之外的任何地方使用它connection.Open(); statement.陈述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM