Skip to main content
 首页 » 编程设计

c#之SSRS DataSourceCredentials 和 Windows 身份验证

2024年02月23日14落叶无声

我有一个应用程序,我在其中通过 SOAP Web 服务呈现 SSRS 报告。我试图做的是将凭据传递给 ReportExecutionService,数据源将使用该凭据进行数据库访问。

目前正在使用 SQL 身份验证。我想弄清楚的是如何让这个为使用 Windows 身份验证的数据库配置的用户工作。用户帐户是域用户帐户,我将此用户添加到数据库和 db_owner 角色。

当我传递 Windows 用户帐户的凭据并运行 SQL Profiler 时,我收到以下错误消息:

Login failed for user 'ourdomain\User'. Reason: Attempting to use an NT account name with SQL Server Authentication. [CLIENT: <local machine>] 

以下代码是(缩写)代码,在传递 SQL 用户凭据时有效,但在传递域用户凭据时失败并显示上述消息:

public static byte[] RunReport(string reportPath, List<ReportParameter> reportParameters, ReportOutputType outputType) 
        { 
            ReportExecutionService reportExecutionService = new ReportExecutionService(); 
 
            var creds = new DataSourceCredentials[1]; 
 
            creds[0] = new DataSourceCredentials() { DataSourceName = "DynamicDataSource", UserName = ReportUserDomain + @"\" + ReportUserAccount, Password = ReportUserPassword }; 
 
            reportExecutionService.LoadReport(reportPath, null); 
 
            reportExecutionService.SetExecutionCredentials(creds); 
 
            return reportExecutionService.Render(Enum.GetName(typeof(ReportOutputType), outputType), GetDeviceInfo(outputType), out extension, out mimetype, out encoding, out warning, out streamids); 
        } 

请注意,我将 ReportUserDomain 参数添加到 DataSourceCredentials 以尝试使其适用于 Windows 身份验证,当前用于 SQL 身份验证的代码不包含此变量。

非常感谢任何帮助。

请您参考如下方法:

我无法找到一种通过 DataSourceCredentials 对象传递 Windows 用户凭据并针对 SQL Server 进行身份验证的方法。貌似这个功能只支持SQL Server认证。

我最终解决问题的方法是将所有报告的数据源设置为“使用 Windows 身份验证(集成安全性)”而不是“提示输入凭据”。然后,我将 DefaultCredentials 从 CredentialCache 传递到 ReportExecutionService,这实际上传递了运行 AppPool 的 Windows 用户的凭据。然后,我授予此 Windows 用户帐户访问数据库和 SSRS 的权限,一切都按预期进行。

不幸的是,这并没有考虑使用与 AppPool 帐户分开的 Windows 用户帐户的凭据的情况。例如,专门用于报告目的的用户帐户。尽管如此,该解决方案还是适合我的需求。