如何在 Web 设置下使用我的登录名而不是我的计算机名通过 Windows 身份验证访问 SQL Server?
我有一个安装 Web 应用程序的 Web 安装项目。在自定义操作中,我正在执行 SQL 脚本来构建数据库。
简单,主要是 MS 示例代码。工作正常。
private void ExecuteSql(string serverName, string dbName, string Sql)
{
System.Data.SqlClient.SqlConnection masterConnection = new System.Data.SqlClient.SqlConnection();
masterConnection.ConnectionString = "Server=" + serverName + "; Database=" + dbName + ";Integrated Security=SSPI";
System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(Sql, masterConnection);
Command.Connection.Open();
Command.Connection.ChangeDatabase(dbName);
try
{
Command.ExecuteNonQuery();
}
finally
{
Command.Connection.Close();
}
}
问题是发送到 SQL Server 的用户是“DOMAIN\MACHINENAME$”,如“AMBER\CHAOS$”而不是“AMBER\corwin”。
如果我添加“AMBER\CHAOS$”作为 SQL Server 上的登录名并授予用户足够的权限,则一切正常。这在最终环境中是不可能的。
那么,我需要做什么才能使我的 Web 设置以“AMBER\corwin”而不是“AMBER\CHAOS$”连接到 SQL 服务器?
I've got a Web Setup project installing a web app. In a Custom Action, I'm executing a SQL script to build a database.
Simple, mostly MS sample code. Works fine.
private void ExecuteSql(string serverName, string dbName, string Sql)
{
System.Data.SqlClient.SqlConnection masterConnection = new System.Data.SqlClient.SqlConnection();
masterConnection.ConnectionString = "Server=" + serverName + "; Database=" + dbName + ";Integrated Security=SSPI";
System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(Sql, masterConnection);
Command.Connection.Open();
Command.Connection.ChangeDatabase(dbName);
try
{
Command.ExecuteNonQuery();
}
finally
{
Command.Connection.Close();
}
}
The problem is that the user that is being sent over to the SQL Server is "DOMAIN\MACHINENAME$", like "AMBER\CHAOS$" rather than "AMBER\corwin".
If I add "AMBER\CHAOS$" as a login on the SQL Server and give the user sufficient rights, everything works fine. That's not possible in the final environment.
So, what do I need to do to make my web setup connect to SQL server as "AMBER\corwin" rather than "AMBER\CHAOS$"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可能会尝试在 *.config 文件中搞乱。您可以尝试使用这些元素:
You might try to mess around in the *.config file. You could try using these elements:
这可能是您网站应用程序池的用户名;
尝试在 /configuration/system.web 内的 web.config 文件中添加:
Probably that's your website application pool username;
Try add into your web.config file, inside /configuration/system.web:
<identity impersonate="true"/>
您必须告诉自定义操作来模拟用户。您如何创建 MSI?使用 WiX,您可以向元素添加: Impersonate="yes"。
You have to tell the custom action to impersonate the user. How are you creating the MSI? With WiX, you would add: Impersonate="yes" to the element.
这可能是因为您网站的应用程序池作为网络服务而不是 Windows 用户运行。将此值更改为您想要连接到数据库的帐户(注意您的应用程序,这可能会根据架构更改您的应用程序)并在 SQL 中添加权限。
This is probably because your web site's application pool is running as Network Service instead of a windows user. Change this value to the account you want to connect to the database as (watch you application, this could change your app depending on the architecture) and add rights in SQL.
只需获取您的连接字符串并使其为:
masterConnection.ConnectionString = "Server=" + serverName + "; Database=" + dbName + ";Integrated Security=SSPI;UserId=" + userId + ";Password=" + password + " ;”;
Simply take your connection string and make it:
masterConnection.ConnectionString = "Server=" + serverName + "; Database=" + dbName + ";Integrated Security=SSPI;UserId=" + userId + ";Password=" + password + ";";