关于sql server:Integrated Security = True和Integrated Security = SSPI有什么区别?

What is the difference between Integrated Security = True and Integrated Security = SSPI?

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

在综合安全的背景下,SSPItrue有什么区别?


根据微软的说法,它们是一样的。

When false, User ID and Password are specified in the connection. When true, the current Windows account credentials are used for authentication.
Recognized values are true, false, yes, no, and sspi (strongly recommended), which is equivalent to true.


Integrated Security=true;不适用于所有SQL提供程序,它在与OleDb提供程序一起使用时抛出异常。

因此,基本上,Integrated Security=SSPI;是首选的,因为它与SQLClientOleDb提供商都合作。

下面是根据msdn-连接字符串语法(ado.net)的完整语法集。

![Windows Auth Syntax


使用Windows身份验证

要连接到数据库服务器,建议使用Windows身份验证,通常称为集成安全性。要指定Windows身份验证,可以将以下两个键值对中的任何一个与数据提供程序一起使用。SQL Server的NET框架:

1
2
 Integrated Security = true;
 Integrated Security = SSPI;

但是,只有第二个与数据提供程序.NET Framework OLEDB一起工作。如果为connectionString设置Integrated Security = true,则会引发异常。

在数据提供程序中指定Windows身份验证。NET Framework for ODBC,您应该使用以下键值对。

1
Trusted_Connection = yes;

源:msdn:使用连接字符串


如果我们使用.Net Reflector来查看SqlConnection的实际代码,许多问题都会得到答案。trueSSPI相同:

1
2
3
4
5
6
7
8
9
10
11
12
13
internal class DbConnectionOptions

...

internal bool ConvertValueToIntegratedSecurityInternal(string stringValue)
{
    if ((CompareInsensitiveInvariant(stringValue,"sspi") || CompareInsensitiveInvariant(stringValue,"true")) || CompareInsensitiveInvariant(stringValue,"yes"))
    {
        return true;
    }
}

...

编辑20.02.2018现在在.NET核心中,我们可以在Github上看到它的开放源代码!搜索ConvertValueToIntegratedSecurityInternal方法:

https://github.com/dotnet/corefx/blob/fdbb160aeb0fad168b363dbdd971d568151a0c8/src/system.data.sqlclient/src/system/data/common/dbconnectionoptions.cs


integrated security=false:在连接中指定用户ID和密码。集成安全性=真:当前的Windows帐户凭据用于身份验证。

集成安全=SSPI:这等于真。

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


让我从Integrated Security = false开始

连接字符串中指定了false用户ID和密码。使用trueWindows帐户凭据进行身份验证。

已确认的值为truefalseyesnoSSPI

如果指定User IDPassword并将集成安全设置为true,则忽略User IDPassword并使用集成安全


请注意,连接字符串特定于您连接到数据的内容和方式。它们正在连接到同一个数据库,但第一个数据库使用的是用于SQL Server的.NET Framework数据提供程序。集成安全性=真对OLEDB无效。

  • 数据源=;初始目录=aspnetdb;集成安全性=true
  • 提供程序=sqloledb;数据源=;集成安全性=sspi;初始目录=aspnetdb

如果有疑问,请使用Visual Studio Server Explorer数据连接。

  • 什么是SSPI?
  • 连接字符串语法

只有在使用.NET sqlclient库时,true才有效。使用OLEDB时无效。其中sspi在这两个库中都是bvaid,要么使用.NET sqlclient库,要么使用OLEDB。


在我看来,

如果您不使用integrated security=sspi,那么您需要在连接字符串中对用户名和密码进行硬编码,这意味着"相对不安全",原因是,所有员工都有访问权限,即使是前员工也可能恶意使用该信息。