我有两个使用集成安全的应用程序。一个在连接字符串中分配 Integrated Security = true
,另一个设置 Integrated Security = SSPI
。
在集成安全性的上下文中,SSPI
和 true
有什么区别?
Integrated Security = True
或 SSPI
不相同。 Integrated Security=true;
不适用于所有 SQL 提供程序,它在与 OleDb
提供程序一起使用时会引发异常。所以基本上 Integrated Security=SSPI;
是首选,因为它同时适用于 SQLClient
和OleDB
提供者。我添加了一个答案以便更好地澄清。
根据Microsoft,它们是同一回事。
如果为 false,则在连接中指定用户 ID 和密码。如果为 true,则使用当前 Windows 帐户凭据进行身份验证。识别值为true、false、yes、no、sspi(强烈推荐),相当于true。
Integrated Security=true;
不适用于所有 SQL 提供程序,它在与 OleDb
提供程序一起使用时会引发异常。
所以基本上 Integrated Security=SSPI;
是首选,因为它同时适用于 SQLClient
和OleDB
提供者。
这是根据 MSDN - Connection String Syntax (ADO.NET) 的完整语法集
https://i.stack.imgur.com/spIRt.jpg
使用 Windows 身份验证
连接数据库服务器推荐使用Windows Authentication,俗称集成安全。要指定 Windows 身份验证,您可以将以下两个键值对中的任何一个与数据提供程序一起使用。 NET 框架用于 SQL Server:
Integrated Security = true;
Integrated Security = SSPI;
但是,只有第二个适用于数据提供程序 .NET Framework OleDb。如果您为 ConnectionString 设置 Integrated Security = true
,则会引发异常。
在数据提供者中指定 Windows 身份验证。 NET Framework for ODBC,您应该使用以下键值对。
Trusted_Connection = yes;
来源:MSDN: Working with Connection Strings
如果我们使用 .Net Reflector
查看 SqlConnection
的实际代码,许多问题都会得到答案 :) true
和 sspi
是相同的:
internal class DbConnectionOptions
...
internal bool ConvertValueToIntegratedSecurityInternal(string stringValue)
{
if ((CompareInsensitiveInvariant(stringValue, "sspi") || CompareInsensitiveInvariant(stringValue, "true")) || CompareInsensitiveInvariant(stringValue, "yes"))
{
return true;
}
}
...
编辑 20.02.2018 现在在 .Net Core 中,我们可以在 github 上看到它的开源!搜索 ConvertValueToIntegratedSecurityInternal 方法:
ConvertValueToIntegratedSecurityInternal
解释的一种情况。该属性仅在提供者为 SqlClient
时使用,因此在 SqlClient
中,SSPI
和 true
相同,但在客户端为 OleDb
或 OracleClient
时不使用。我已经在 stackoverflow.com/a/23637478/704008 中通过 msdn 参考说明了这一点
Integrated Security = False:在连接中指定了用户 ID 和密码。 Integrated Security = true :当前 Windows 帐户凭据用于身份验证。
Integrated Security = SSPI :这等价于 true。
我们可以避免连接字符串中的用户名和密码属性,并使用集成安全性
让我从Integrated Security = false
开始
false
用户 ID 和密码在连接字符串中指定。
true
Windows 帐户凭据用于身份验证。
可识别的值为 true
、false
、yes
、no
和 SSPI
。
如果指定了 User ID
和 Password
并将 Integrated Security 设置为 true
,则 User ID
和 Password
将被忽略并使用 Integrated Security
请注意,连接字符串特定于您连接数据的内容和方式。它们连接到同一个数据库,但第一个是使用 .NET Framework Data Provider for SQL Server。 Integrated Security=True 不适用于 OleDb。
数据源=.;初始目录=aspnetdb;集成安全=True
提供者=SQLOLEDB;数据源=.;集成安全=SSPI;初始目录=aspnetdb
如有疑问,请使用 Visual Studio Server Explorer 数据连接。
什么是sspi?
连接字符串语法
True 仅在您使用 .NET SqlClient 库时有效。使用 OLEDB 时无效。 SSPI 在您使用 .net SqlClient 库或 OLEDB 时都适用。
在我看来,
如果您不使用 Integrated security=SSPI,那么您需要在连接字符串中对用户名和密码进行硬编码,这意味着“相对不安全”,因为所有员工都可以访问,甚至前员工也可以恶意使用这些信息。
不定期副业成功案例分享