SQL Server Reporting Service (SSRS) – HTTP Status 401 Unauthorized Error
I am going to continue on writing article on SQL Server Reporting Services (Its because, it is providing me good fame on http://forums.asp.net, LOL).
We have to deploy the reports on the the Reporting Services Virtual Directory (I can write the whole different article on the Reporting Deployment, but may be next time. Right now we don’t want to go deep into that.). The Reporting Server Virtual Directory may exist on the different server. It is recommended to keep anonymous access off for Reports virtual directory if you are going to call reports on the web.
The anonymous access allows any user to access the page. But now when we are keeping anonymous access off for the Reports, it can create problem while fetching the report inside the Report Viewer control on any ASPX page.
The Report server will expect authorized user to access the reports. This can be done by two ways.
- Passing the Report Server User Credentials with the Reports.
- Forms Authentication on Reporting Server.
Today, I am going to explain that How can we pass the User Credentials of the Reporting Server with the report call.
We need to Create one sealed class to perform this action. This class need to be inherited from IReportServerCredential interface.
[Serializable]
public sealed class ReportServerNetworkCredentials : IReportServerCredentials
{
#region IReportServerCredentials Members
/// <summary>
/// Provides forms authentication to be used to connect to the report server.
/// </summary>
/// <param name="authCookie">A Report Server authentication cookie.</param>
/// <param name="userName">The name of the user.</param>
/// <param name="password">The password of the user.</param>
/// <param name="authority">The authority to use when authenticating the user, such as a Microsoft Windows domain.</param>
/// <returns></returns>
public bool GetFormsCredentials(out System.Net.Cookie authCookie, out string userName,
out string password, out string authority)
{
authCookie = null;
userName = null;
password = null;
authority = null;
return false;
}
/// <summary>
/// Specifies the user to impersonate when connecting to a report server.
/// </summary>
/// <value></value>
/// <returns>A WindowsIdentity object representing the user to impersonate.</returns>
public WindowsIdentity ImpersonationUser
{
get
{
return null;
}
}
/// <summary>
/// Returns network credentials to be used for authentication with the report server.
/// </summary>
/// <value></value>
/// <returns>A NetworkCredentials object.</returns>
public System.Net.ICredentials NetworkCredentials
{
get
{
string userName = "SERVERUSERNAME";
string domainName = "DOMAIN";
string password = "somepassword";
return new System.Net.NetworkCredential(userName, password, domainName);
}
}
#endregion
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
How to Utilize the ReportServerNetworkCredentials Class in your Report call on Page_Load event…
protected void Page_Load(object sender, EventArgs e) { myReportViewer.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote; // Here we are going to pass the ReportServerCredentials to the Report Viewer. myReportViewer.ServerReport.ReportServerCredentials = new ReportServerNetworkCredentials(); ReportServerLoaction = ConfigurationManager.AppSettings["REPORT_SERVER_PATH"]; myReportViewer.ServerReport.ReportServerUrl = new Uri("http://MyReportServer/Reports/"); myReportViewer.ServerReport.ReportPath = "TempReports/MyFirstReport"; myReportViewer.ShowParameterPrompts = false; myReportViewer.ShowPrintButton = true; Microsoft.Reporting.WebForms.ReportParameter[] reportParameterCollection = new Microsoft.Reporting.WebForms.ReportParameter[1]; reportParameterCollection[0] = new Microsoft.Reporting.WebForms.ReportParameter(); reportParameterCollection[0].Name = "ClientID"; reportParameterCollection[0].Values.Add("49020644-63AA-4D92-81A1-8F85D49ACF67"); myReportViewer.ServerReport.SetParameters(reportParameterCollection); myReportViewer.ServerReport.Refresh(); }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
Please feel free to comment on the article. Critics are highly appreciated.
8 comments so far
Leave a reply
I have posted a question on the http://forums.asp.net as well.
string userName = “SERVERUSERNAME”;
string domainName = “DOMAIN”;
string password = “somepassword”;
Where on the remote server containing the SSRS do you set up this user? The Management console only allows to set up roles, and there is not place for a password.. Confused.
Thank you
Hi Alokazia,
Extremely sorry for the late reply. I was out of my city with very limited Internet connectivity.
If you are using Server OS like Windows 2000 server or Windows 2003 as the SSRS host, you will need to provide the credentials of that server. This credentials need to be such user, who have enough rights to access and modified the reports.
I am not sure about the server you are using. Please provide me the information about the SSRS server OS, so I can guide you to setup a user account on that.
Regards,
Nirav Patel
i would like more info on where i need to paste this code?
we are running sql 2005 reporting services and after taking the annonymious off and use windows authentication we click to review a report and it gives us the this error. i just need help on adding this code to see if this will work i have visual studio 2008 and im learning c# so im some what educated..
thank yoiu for your time
-summey
Hi Rayan,
Sorry for late reply, are you still facing this issue. Please let me know, so I can help you in that…
I receive an error while inheriting IPCredential class
error “The type or namespace name ‘IReportServerCredentials’ could not be found (are you missing a using directive or an assembly reference?)”
Hi Anand,
You can find the IReportServerCredentials from Microsoft.ReportViewer.WebForms assembly. Please try to add that to your project and let me know if you still face any issue.
And I am extremely sorry for delay in reply.
Thank you for the reply, no worries about how long its been i appreciate you getting back with me anyways. Yes i found a solution.
Thank you again for takeing your time to help.
-Summey
Hey this is really great, you have saved my time. great code