我正在开发一个需要运行 Crystal Reports 的应用程序。数据库连接字符串是动态的,并且连接是在运行时建立的。有多个数据库可以引用,因此连接是在运行时分配的。
要访问特定数据库,需要使用 Active Directory 用户授予访问权限。该用户有权使用集成安全性查询数据库,但无法以普通 Windows 用户身份登录。此外,他们无权登录运行该应用程序的客户端计算机。运行应用程序的用户无权访问数据库。
通过模拟,我成功地对数据库进行了查询。但是,我无法让 Crystal Reports 以具有访问权限的用户身份运行。
Crystal Reports 报表的连接在打开之前已分配,如下所示:
private void AssignDataSource(string connString)
{
SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder(connString);
if (csb.IntegratedSecurity)
{
report.DataSourceConnections[0].SetConnection(csb.DataSource, csb.InitialCatalog, true);
}
else
{
report.DataSourceConnections[0].SetConnection(csb.DataSource, csb.InitialCatalog, csb.UserID, csb.Password);
}
}
但是,这样,就不会应用正确用户的凭据。相反,它会尝试应用运行该应用程序的用户的凭据,而该用户没有访问权限。
我打算克服这个障碍的方法是通过像这样调用报告,将报告的整个创建工作委托给具有数据库访问权限的用户:
LibreriaGenerica.Clases.Impersonation.ImpersonationHelper.Impersonate<object>(user, pass, () =>
{
FormPrevisualizadorReporteGenerico p = new FormPrevisualizadorReporteGenerico(new crReporteDiagnosticoConexion(), BDManager.GetInstanceGICO().GetCadenaConexion());
p.ShowDialog();
return null;
});
但是,由于该用户没有登录客户端计算机的权限,因此出现以下错误:
System.IO.FileLoadException:“无法加载文件或程序集” 'ReportesGenericos,版本=1.0.0.0,文化=中性, PublicKeyToken=null' 或其依赖项之一。访问被拒绝。'
有什么建议吗?
这就是我在网络应用程序中进行模拟的方式。
'+++ Impersonation
Dim currentWindowsIdentity As WindowsIdentity = CType(User.Identity, WindowsIdentity)
Dim impersonationContext As WindowsImpersonationContext
If Not String.IsNullOrEmpty(currentWindowsIdentity.Name) Then
impersonationContext = currentWindowsIdentity.Impersonate
'Insert your code that runs under the security context of the authenticating user here.
impersonationContext.Undo()
Else
'Unknown user. Probably in debugger.
'Insert your code
End If
'---