连接字符串使用Windows身份验证。

206

我正在创建一个网站,但在数据库中我使用Windows身份验证。

我知道你使用这个来进行SQL身份验证。

<connectionStrings> 
    <add name="NorthwindContex" 
       connectionString="data source=localhost;
       initial catalog=northwind;persist security info=True; 
       user id=sa;password=P@ssw0rd" 
       providerName="System.Data.SqlClient" /> 
</connectionStrings>

我该如何修改以适用于Windows身份验证?


不确定您是否希望使用通用的Windows账户供不同的人使用。 - rchacko
5个回答

277
< p >将用户名和密码替换为Integrated Security=SSPI;

这样连接字符串应该是:

<connectionStrings> 
<add name="NorthwindContex" 
   connectionString="data source=localhost;
   initial catalog=northwind;persist security info=True; 
   Integrated Security=SSPI;" 
   providerName="System.Data.SqlClient" /> 
</connectionStrings> 

1
我知道你可以将特定的AD用户设置为应用程序池(Web应用程序)。你能否对Windows应用程序执行相同的操作? - user384080
9
“Persist Security Info” 可能是不必要的:https://dev59.com/33I-5IYBdhLWcg3wHUdB#2010059。 - Sphinxxx
@heads5150:我的项目中可能没有连接字符串吗?我有什么遗漏的地方吗?我已经搜索了整个解决方案,以找到像上面那样的连接字符串,但是我找不到任何东西。我发现的那一个在Web Release和Web Config中都被注释掉了。我正在使用带有本地数据库的VS Express 2013。 - Vini

46

通过Windows身份验证连接到SQL Server数据库基本上需要确定要连接的服务器、数据库名称、集成安全信息和提供程序名称。

基本上,这是有效的:

<connectionStrings>      
<add name="MyConnectionString"
         connectionString="data source=ServerName;
   Initial Catalog=DatabaseName;Integrated Security=True;"
         providerName="System.Data.SqlClient" />
</connectionStrings> 

设置Integrated Security字段的值为true基本上意味着您想通过Windows身份验证访问数据库,如果将此字段设置为false,则Windows身份验证将不起作用。

它还根据您使用的提供程序而有所不同。

  • SqlClient中,Integrated Security=true;IntegratedSecurity=SSPI;都可以使用。

  • OleDb中,使用Integrated Security=SSPI;

  • Odbc中,使用Trusted_Connection=yes;
  • OracleClient中,使用Integrated Security=yes;

在OleDb提供程序中使用Integrated Security=true会引发异常。


30

在经过多个小时的努力后,找到了正确的解决方案:

  1. 打开配置文件
  2. 将连接字符串更改为以下内容

<add name="umbracoDbDSN" connectionString="data source=YOUR_SERVER_NAME;database=nrc;Integrated Security=SSPI;persist security info=True;" providerName="System.Data.SqlClient" />

  1. YOUR_SERVER_NAME更改为当前服务器名称并保存
  2. 打开IIS管理器
  3. 找到网站或Web应用程序使用的应用程序池的名称
  4. 右键单击并选择“高级设置”
  5. 在高级设置下的进程模型中,将身份更改为自定义帐户并添加您的服务器管理员详细信息,请参阅附图:

enter image description here


4
这个解决方案对我很有效,但我想知道这个身份验证的变化会如何影响应用程序的行为,特别是安全方面? - CesarB
进程执行的所有操作都将使用该帐户的权限/特权运行。不要授予比所需更多的权限。建议使用专用服务帐户。还建议查看DISA IIS和Windows Server STIG:https://public.cyber.mil/stigs/downloads/ - duct_tape_coder
解决方案可行,但需要补充说明的是,当您尝试设置自定义帐户时,您要添加的帐户应该已经存在于控制面板>用户帐户中,否则您将会收到错误提示(如果您尝试添加一个在控制面板>用户帐户中不存在的帐户)。因此,请先在用户帐户中添加该帐户,然后您才能将其设置为应用程序标识。 - m_beta

12

这更短并且有效

<connectionStrings>      
<add name="DBConnection"
             connectionString="data source=SERVER\INSTANCE;
       Initial Catalog=MyDB;Integrated Security=SSPI;"
             providerName="System.Data.SqlClient" />
</connectionStrings> 

不需要持久化安全信息


8
如果有人寻找asp.net core,我们需要在appsettings.json中添加连接字符串。
 {
"ConnectionStrings": {
   "DefaultConnection": "Server=SQLServer\\Instance;Database=MYDB;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}

来源:如何添加Windows身份验证SQL Server连接字符串


1
TrustServerCertificate=true 如果您在.NET Core 6中使用Windows登录时遇到“已成功与服务器建立连接,但在登录过程中发生错误。(提供程序: SSL提供程序,错误: 0 - 证书链是由不受信任的授权机构颁发的。)”,请注意以上设置。 - divay pandey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接