我有两个使用集成安全性的应用程序。一个在连接字符串中指定Integrated Security = true
,另一个设置Integrated Security = SSPI
。
在综合安全的背景下,SSPI
和true
有什么区别?
据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),这是完整的语法集
使用Windows身份验证
建议使用Windows身份验证(通常称为集成安全性)连接到数据库服务器。要指定Windows身份验证,可以将以下两个键值对中的任何一个与数据提供程序一起使用。用于SQL Server的.NET Framework:
Integrated Security = true;
Integrated Security = SSPI;
但是,只有第二个适用于数据提供程序.NET Framework OleDb。如果为ConnectionString设置Integrated Security = true
,则抛出异常。
在数据提供程序中指定Windows身份验证。在.NET Framework for ODBC中,您应该使用以下键值对。
Trusted_Connection = yes;
如果我们使用.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方法:
Integrated Security = False:在连接中指定用户ID和密码。 Integrated Security = true:当前Windows帐户凭据用于身份验证。
集成安全性= SSPI:这与真实相当。
我们可以避免连接字符串中的用户名和密码属性,并使用集成安全性
让我先从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不起作用。
如有疑问,请使用Visual Studio Server Explorer数据连接。
True仅在您使用.NET SqlClient库时才有效。使用OLEDB时无效。无论您使用.net SqlClient库还是OLEDB,SSPI都是bvaid。
在我看来,
如果你不使用集成安全性= SSPI,那么你需要在连接字符串中硬编码用户名和密码,这意味着“相对不安全”为什么,因为所有员工都有访问权限甚至前雇员都可以恶意使用这些信息。