.net 使用 Windows 身份验证的连接字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14270082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 16:49:05  来源:igfitidea点击:

Connection string using Windows Authentication

.netasp.net-mvcconnection-stringwindows-authentication

提问by MDC

I am creating a website, but in the database I use windows authentication.

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

I know that you use this for SQL authentication

我知道你用它来进行 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>

How do I modify this to work with windows authentication?

如何修改它以使用 Windows 身份验证?

回答by heads5150

Replace the username and password with Integrated Security=SSPI;

将用户名和密码替换为 Integrated Security=SSPI;

So the connection string should be

所以连接字符串应该是

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

回答by Ahmed Na.

For the correct solution after many hours:

几个小时后的正确解决方案:

  1. Open the configuration file
  2. Change the connection string with the following
  1. 打开配置文件
  2. 使用以下内容更改连接字符串

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

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

  1. Change the YOUR_SERVER_NAMEwith your current server name and save
  2. Open the IIS Manager
  3. Find the name of the application pool that the website or web application is using
  4. Right-click and choose Advanced settings
  5. From Advanced settings under Process Modelchange the Identityto Custom accountand add your Server Admin details, please see the attached images:
  1. 使用您当前的服务器名称更改YOUR_SERVER_NAME并保存
  2. 打开 IIS 管理器
  3. 查找网站或 Web 应用程序正在使用的应用程序池的名称
  4. 右键单击并选择高级设置
  5. 流程模型下的高级设置中,将身份更改为自定义帐户并添加您的服务器管理员详细信息,请参阅附加图像:

enter image description here

在此处输入图片说明

Hope this will help.

希望这会有所帮助。

回答by nzrytmn

For connecting to a sql server database via Windows authentication basically needs which server you want to connect , what is your database name , Integrated Security info and provider name.

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

Basically this works:

基本上这有效:

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

Setting Integrated Securityfield truemeans basically you want to reach database via Windows authentication, if you set this field falseWindows authentication will not work.

Integrated Security字段设置为true意味着您希望通过 Windows 身份验证访问数据库,如果将此字段设置为false,Windows 身份验证将不起作用。

It is also working different according which provider you are using.

根据您使用的提供商,它的工作方式也不同。

  • SqlClientboth Integrated Security=true;or IntegratedSecurity=SSPI;is working.

  • OleDbit is Integrated Security=SSPI;

  • Odbcit is Trusted_Connection=yes;
  • OracleClientit is Integrated Security=yes;
  • SqlClient集成安全=true; IntegratedSecurity=SSPI;正在工作。

  • OleDbIntegrated Security=SSPI;

  • OdbcTrusted_Connection=yes;
  • OracleClientIntegrated Security=yes;

Integrated Security=true throws an exception when used with the OleDb provider.

与 OleDb 提供程序一起使用时,Integrated Security=true 会引发异常。

回答by Carlos E

This is shorter and works

这更短并且有效

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

Persist Security Info not needed

不需要持久安全信息