如何在 Web 设置下使用我的登录名而不是我的计算机名通过 Windows 身份验证访问 SQL Server?

发布于 2024-08-06 15:48:33 字数 949 浏览 3 评论 0原文

我有一个安装 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

别想她 2024-08-13 15:48:34

您可能会尝试在 *.config 文件中搞乱。您可以尝试使用这些元素:

<system.web>   
  <authentication mode="Windows" />   
  <identity impersonate="true" /> 
</system.web>

You might try to mess around in the *.config file. You could try using these elements:

<system.web>   
  <authentication mode="Windows" />   
  <identity impersonate="true" /> 
</system.web>
似梦非梦 2024-08-13 15:48:34

这可能是您网站应用程序池的用户名;
尝试在 /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"/>

魂牵梦绕锁你心扉 2024-08-13 15:48:34

您必须告诉自定义操作来模拟用户。您如何创建 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.

小鸟爱天空丶 2024-08-13 15:48:34

这可能是因为您网站的应用程序池作为网络服务而不是 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.

ゃ懵逼小萝莉 2024-08-13 15:48:34

只需获取您的连接字符串并使其为:

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 + ";";

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文