3

我想使用集成身份验证从 Web 部件访问 SQL 数据库。它应该使用 IIS 应用程序池标识。

默认情况下,您将收到错误消息:

System.Data.SqlClient.SqlException: Login failed for user 'SERVER\IUSR_VIRTUALMACHINE'.

因为在 web.config 中模拟设置为 true:

<identity impersonate="true" />

我可以将其设置为 false 并且数据库代码将起作用。匿名访问的网站也可以使用。但是,任何使用身份验证的 SharePoint 网站都会失败,因此这并不是真正的解决方案。

为了解决这个问题,我必须封装我的所有数据库访问代码以使用提升的权限运行,这就是 SharePoint 在内部的方式吗?不知何故,这似乎不是最高效的解决方案。这仍然是要走的路吗,只需使用 SQL 安全性从 SharePoint 自定义 Web 部件访问数据库?

4

3 回答 3

4

web.config 文件中的<identity /><authentication />元素将共同确定在使用集成身份验证时用于连接到 SQL Server 的帐户。

配置后<authentication mode="Windows" />,您将推迟到 IIS 对用户进行身份验证。我猜你的 web.config 包含:

<authentication mode="Windows" />
<identity impersonate="true" />

and that IIS is configured to allow anonymous users. Setting <identity impersonate="true" /> causes IIS to pass the identity of the IIS anonymous access account to SQL Server.

As Lars point out, using SPSecurity.RunWithElevatedPrivileges will achieve what you want. I don't believe you'll see any noticeable impact on performance but that's something you can test :-)

于 2009-06-30T07:30:07.673 回答
3

使用SPSecurity.RunWithElevatedPrivileges在应用程序池标识的上下文中运行您的代码。

于 2009-06-30T07:19:25.260 回答
0

这是不正确的。因为<identity impersonate="true" />设置为 true ASP.NET / IIS 将以当前登录的用户身份运行线程(因此不是应用程序池帐户,而是实际登录网站的用户)。

这里正在发生其他事情。您可以发布自定义数据库的连接字符串吗?(减去私人数据)

于 2009-06-30T05:59:44.637 回答