我有两个使用集成安全性的应用程序。一个在连接字符串中分配 Integrated Security = true
,另一个设置 Integrated Security = SSPI
。
在集成安全性的上下文中,SSPI
和true
有什么区别?
我有两个使用集成安全性的应用程序。一个在连接字符串中分配 Integrated Security = true
,另一个设置 Integrated Security = SSPI
。
在集成安全性的上下文中,SSPI
和true
有什么区别?
Integrated Security=true;
这个选项并不适用于所有的SQL提供程序,在使用OleDb
提供程序时会抛出异常。
因此,基本上最好选择Integrated Security=SSPI;
,因为它可以与SQLClient
和OleDB
提供程序一起使用。
以下是完整的语法设置,根据MSDN-Connection String Syntax(ADO.NET)
使用Windows身份验证
连接数据库服务器建议使用Windows身份验证,通常称为集成安全性。要指定Windows身份验证,可以使用以下两个键值对之一与数据提供程序一起使用.NET Framework for SQL Server:
Integrated Security = true;
Integrated Security = SSPI;
然而,只有第二种方法适用于数据提供程序.NET Framework OleDb。如果您为ConnectionString设置Integrated Security=true
,将会抛出异常。
要在数据提供程序.NET Framework for ODBC中指定Windows身份验证,您应该使用以下键值对。
Trusted_Connection = yes;
来源:MSDN:使用连接字符串
如果我们使用.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;
}
}
...
编辑于 2018年2月20日
现在在 .Net Core 中,我们可以在Github上看到它的开源!搜索 ConvertValueToIntegratedSecurityInternal 方法:
https://github.com/dotnet/corefx/blob/fdbb160aeb0fad168b3603dbdd971d568151a0c8/src/System.Data.SqlClient/src/System/Data/Common/DbConnectionOptions.csIntegrated 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 SqlClient库时,True才是有效的。在使用OLEDB时它无效。 在使用.net SqlClient库或OLEDB时,SSPI都是有效的。