Visualizzatore report SSRS + credenziali ASP.NET 401 eccezione
-
10-07-2019 - |
Domanda
Ho un report salvato su un server di report SQL2005 e desidero restituire un PDF renderizzato di questo report. L'ho capito lavorando con un file * .rdlc locale ( e ne ho scritto un blog ), ma non quando * .rdl risiede su un server di report. Ricevo un errore 401 non autorizzato sulla riga ...
reportViewer.ServerReport.SetParameters(reportDefinition.ReportParameters);
Ecco il metodo utilizzato per eseguire il rendering del rapporto.
public byte[] Render(IReportDefinition reportDefinition)
{
var reportViewer = new ReportViewer();
byte[] renderedReport;
try
{
var credentials = new WindowsImpersonationCredentials();
reportViewer.ServerReport.ReportServerUrl = new Uri("http://myssrsbox", UrlKind.Absolute);
reportViewer.ServerReport.ReportServerCredentials = credentials;
reportViewer.ServerReport.ReportPath = reportDefinition.Path;
// Exception is thrown on the following line...
reportViewer.ServerReport.SetParameters(reportDefinition.ReportParameters);
string mimeType;
string encoding;
string filenameExtension;
string[] streams;
Warning[] warnings;
renderedReport = reportViewer.ServerReport.Render(reportDefinition.OutputType, reportDefinition.DeviceInfo, out mimeType, out encoding, out filenameExtension, out streams, out warnings);
}
catch (Exception ex)
{
// log the error...
throw;
}
finally
{
reportViewer.Dispose();
}
return renderedReport;
}
L'altra cosa che ti manca è la classe WindowsImpersonationCredentials.
public class WindowsImpersonationCredentials : IReportServerCredentials
{
public bool GetFormsCredentials(out Cookie authCookie, out string userName, out string password, out string authority)
{
authCookie = null;
userName = password = authority = null;
return false;
}
public WindowsIdentity ImpersonationUser
{
get { return WindowsIdentity.GetCurrent(); }
}
public ICredentials NetworkCredentials
{
get { return null; }
}
public override string ToString()
{
return String.Format("WindowsIdentity: {0} ({1})", this.ImpersonationUser.Name, this.ImpersonationUser.User.Value);
}
}
Altre cose che potresti aver bisogno di sapere ...
- Questo è in esecuzione su una rete Intranet e la rappresentazione è attivata.
- La registrazione indica che l'utente della rappresentazione è impostato correttamente.
- Questo funziona quando viene eseguito in Visual Studio (
http: // localhost: devport
) e funziona quando è in esecuzione sul mio box di sviluppo (http: // localhost / myApplication
). non funziona durante l'esecuzione sui nostri server di prova o di produzione. - Ho provato soluzioni con e senza le impostazioni di system.net.defaultProxy in web.config. Né ha funzionato.
Cosa sto facendo di sbagliato? È un'impostazione del server? È codice? È web.config?
Soluzione
Abbiamo finalmente capito il problema. I nostri amministratori di rete hanno disabilitato il doppio hopping, quindi mentre la rappresentazione si connetteva correttamente come domain \ jmeyer
, l'applicazione stava ancora tentando di connettersi alla casella SRS con domain \ web01 $
. Perché è impostato in questo modo? Perché il doppio salto è un enorme buco di sicurezza. (O almeno così mi è stato detto. Sembra qualcosa che vorresti leggere su The Daily WTF ?)
La nostra soluzione era creare un dominio \ ssrs_report_services
generico e connettersi con quell'utente con le seguenti credenziali di rete
public class CustomCredentials : IReportServerCredentials
{
public bool GetFormsCredentials(out Cookie authCookie, out string userName, out string password, out string authority)
{
authCookie = null;
userName = password = authority = null;
return false;
}
public WindowsIdentity ImpersonationUser
{
get { return null; }
}
public ICredentials NetworkCredentials
{
get { return new NetworkCredential("ssrs_report_services", "password", "domain") ; }
}
}
Quanto sopra è la classica soluzione di esempio che puoi trovare in tutti gli internet.
Altri suggerimenti
" Double hopping " è consentito - con l'autenticazione Kerberos ... (purché funzioni correttamente!)