Integrated Security = True 和 Integrated Security = SSPI 有什么区别?

629

我有两个使用集成安全性的应用程序。一个在连接字符串中分配 Integrated Security = true,另一个设置 Integrated Security = SSPI

在集成安全性的上下文中,SSPItrue有什么区别?


84
接受的答案不是最佳的,也不完全正确。Integrated Security = True或SSPI并不相同。“Integrated Security=true;”不能在所有SQL提供程序中使用,当与“OleDb”提供程序一起使用时会抛出异常。因此,基本上“Integrated Security=SSPI;”更可取,因为可以与“SQLClient”和“OleDB”提供程序一起使用。我已经添加了一个答案以更好地澄清。 - Pranav Singh
4
@PranavSingh 的想法是对的,除非你明确指定你正在使用哪个“提供商”,否则这个问题是不完整的。不同的提供商接受和/或将各种字符串转换为内部状态。 - Mark
SSPI是什么意思?“SS”希望代表SQL Server,但不确定“SI”代表什么。 - ATL_DEV
@PatrickMcDonald 嗯? - ATL_DEV
@抱歉,ET的评论是一个不好笑的玩笑。 - Patrick McDonald
显示剩余3条评论
9个回答

489
根据 Microsoft 的说法,它们是同一件事。
当值为 false 时,在连接中指定用户 ID 和密码。当值为 true 时,使用当前 Windows 帐户凭据进行身份验证。 已识别的值包括 true、false、yes、no 和 sspi(强烈建议),该值相当于 true。

35
最初,我认为“True”使用NTLM,“SSPI”使用Kerberos,但现在它们是可以互换的。 - SqlRyan
5
没有检查上一个评论,但如果是真的,应该作为答案,而不是评论。 - Johnny_D
21
@RodneyFoley 抱歉,我的测试确认这个答案是正确的,而你的评论则不正确。也许以前它是有效的,但现在不再是了,你也无法提供任何微软文档支持你的观点。 - Kirk Broadhurst
3
同意 Kirk 的观点。当使用 SSPI 指定身份验证时,用户/密码将被忽略 - .net 4.0、SQL Server 2012。 - Alex des Pelagos
8
如果它们“是同一件事”,为什么SSPI被“强烈推荐”而不是“真实”或“是”的呢?这就是我提出这个问题的原因... - Zé Carlos
显示剩余9条评论

241

Integrated Security=true;这个选项并不适用于所有的SQL提供程序,在使用OleDb提供程序时会抛出异常。

因此,基本上最好选择Integrated Security=SSPI;,因为它可以与SQLClientOleDB提供程序一起使用。

以下是完整的语法设置,根据MSDN-Connection String Syntax(ADO.NET)

![Windows Auth Syntax


这个答案不是重复了第三个评级的吗? - Yola
2
@Yola 这个答案更加完整,还链接到一个仍然有效的 Microsoft Docs 页面(另一个答案中的链接现在会带你到一个建议下载 Visual Studio 2005 已退役文档的页面)。 - interDist

80

使用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:使用连接字符串


37

如果我们使用.Net Reflector查看SqlConnection的实际代码,许多问题将得到答案 :) truesspi是相同的:

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.cs

4
这段代码仅适用于一个可通过名称“ConvertValueToIntegratedSecurityInternal”解释的情况。该属性仅在提供程序为“SqlClient”时使用,因此在“SqlClient”中,“SSPI”和“true”是相同的,但当客户端为“OleDb”或“OracleClient”时不同。我已在https://dev59.com/fXM_5IYBdhLWcg3wyWWt#23637478中通过MSDN参考进行了澄清。 - Pranav Singh

27

Integrated Security = False:在连接中指定用户ID和密码。

Integrated Security = true:使用当前Windows账户凭据进行身份验证。

Integrated Security = SSPI:等同于true。

我们可以避免在连接字符串中使用用户名和密码属性,而使用集成安全性。


19

让我从Integrated Security = false开始说起。

false表示连接字符串中指定了用户ID和密码。
true表示使用Windows账户凭据进行身份验证。

可识别的值包括truefalseyesnoSSPI

如果指定了User IDPassword,并且将Integrated Security设置为true,则会忽略User IDPassword,并使用Integrated Security。


8
请注意,连接字符串是特定于您连接数据的方式内容。这些都连接到同一个数据库,但第一个使用的是.NET Framework Data Provider for SQL Server。Integrated Security=True在OleDb中不起作用。
  • Data Source=.;Initial Catalog=aspnetdb;Integrated Security=True
  • Provider=SQLOLEDB;Data Source=.;Integrated Security=SSPI;Initial Catalog=aspnetdb
如果不确定,请使用Visual Studio Server Explorer Data Connections。

7

只有在使用.NET SqlClient库时,True才是有效的。在使用OLEDB时它无效。 在使用.net SqlClient库或OLEDB时,SSPI都是有效的。


https://social.msdn.microsoft.com/Forums/en-US/b61c092b-48c8-4bc7-b26b-47b41ea72b69/is-there-a-difference-bt-integrated-security-true-and-integrated-security-sspi?forum=adodotnetdataproviders - Amit Shishodia

0
在我看来,如果您不使用Integrated security=SSPI,则需要在连接字符串中硬编码用户名和密码,这意味着“相对不安全”,因为所有员工都可以访问,即使前员工也可能恶意使用信息。

3
连接字符串不一定对所有员工可见。 - underscore_d
1
如果您不使用“Integrated security=SSPI”,则可以使用“Integrated security=true”,而无需硬编码用户名和密码。此外,保护这些类型的配置超出了本问题的范围。 - Zimano

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