Tuesday, February 21, 2012

reportviewer: code the windows credentials

I have bought a hosting service for reporting services. The server definitions are set to windows authentication.

I'm using a small application and each time I access a report, a windows prompt asks for the username and password. I would like to avoid this message.

How should I pass the credentials through this application and the reportviewer control (which I use) to the server in order to avoid having to insert the user details in the prompt. This application is to be used by several users and they shouldn't need to insert the account's information.

thank you for a quick reply.

There is no way to add credentials to the report viewer.

Does your local login map to a login on the server at all? If so, Internet Explorer only sends credentials automatically to servers in the Intranet zone, but you can have it automatically send credentials to your host.

You could mark your host as a trusted site (Tools -> Internet Options -> Security), and then tell IE to send credentials automatically to sites in the trusted zone. With Trusted Zones highlighted in the tab, click "Custom Level". Scroll down to the bottom and select "automatic login with current username and password".

|||This can be done. First create a class called ReportServerCredentials that implements the IReportServerCredentials interface. You will need to import System.Net and Microsoft.Reporting.WebForms to this class. Overload the New and GetFormsCredentials methods and the NetworkCredentials and ImpersonationUser properties. Here's a class that I use:

Imports Microsoft.VisualBasic
Imports Microsoft.Reporting.WebForms
Imports System.Net

Public Class ReportServerCredentials
Implements IReportServerCredentials

Private _userName As String
Private _password As String
Private _domain As String

Public Sub New(ByVal userName As String, ByVal password As String, ByVal domain As String)
_userName = userName
_password = password
_domain = domain
End Sub

Public ReadOnly Property ImpersonationUser() As System.Security.Principal.WindowsIdentity Implements Microsoft.Reporting.WebForms.IReportServerCredentials.ImpersonationUser
Get
Return Nothing
End Get
End Property

Public ReadOnly Property NetworkCredentials() As ICredentials Implements Microsoft.Reporting.WebForms.IReportServerCredentials.NetworkCredentials
Get
Return New NetworkCredential(_userName, _password, _domain)
End Get
End Property

Public Function GetFormsCredentials(ByRef authCookie As System.Net.Cookie, ByRef userName As String, ByRef password As String, ByRef authority As String) As Boolean Implements Microsoft.Reporting.WebForms.IReportServerCredentials.GetFormsCredentials
userName = _userName
password = _password
authority = _domain
Return Nothing
End Function
End Class

Next, in the OnLoad of your page with the ReportViewer control, create a new instance of your ReportServerCredentials (I use values stored in AppSettings). Then assign your object to the ReportViewer control's ServerReport.ReportServerCredentials property.

Dim cred As New ReportServerCredentials(ConfigurationManager.AppSettings("MyStoredUser"),_
ConfigurationManager.AppSettings("MyStoredPassword"), _
ConfigurationManager.AppSettings("MyStoredDomain"))

MyReportViewerControl.ServerReport.ReportServerCredentials = cred

A few things can break though-- referenced images on your reports won't render, and you can't use integrated security in your SSRS data sources, unless the account you are impersonating has access to the data as well as SSRS.
|||

Code Salad,

My immeasurable thanks. After three days of searching, your solution was the one that finally got my reports running

|||

It seems CodeSalad's code worked. Can someone explain how it works? Where do I add the code? I am having the same problem. I am using Framework 2.0. Thanks.

DanYeung

|||

I think these links will be helpful -

http://blogs.msdn.com/bimusings/archive/2005/11/18/494436.aspx

http://msdn2.microsoft.com/en-us/library/aa983458(VS.80).aspx

|||

Danyeng,

U have to add a new class file in the app_Code directory

|||

The links and the sample code are very helpful. I still need more help on this. How do I get the ReportViewer control? I am opening the report without using ReportView control. I added the attribute to the button to open the report with Javascript. If I use the ReportViewer control, do I have to open the report differently and how? Thanks.

DanYeung

|||

this is how u doing it.

create a new aspx page and add the report viewer control to ur page by going View Menu -> Toolbox and in the toolbox look under Data.. and then in the properties window, Select the processingmode i.e Remote or local and also specify the report path...

and then in the aspx.vb page in Page_Load sub give this code.

ReportViewer1.ProcessingMode = ProcessingMode.Remote ReportViewer1.ShowCredentialPrompts =True'ReportViewer1.ShowExportControls = False ReportViewer1.ServerReport.ReportServerCredentials =New ReportServerCredentials() ReportViewer1.ServerReport.ReportServerUrl =New Uri("http://IPAddress/ReportServer") ReportViewer1.ServerReport.ReportPath ="/FolderName/Mainrdlfilename" ReportViewer1.ServerReport.Refresh()

ReportViewer1.ServerReport.ReportServerCredentials =New ReportServerCredentials() -- ReportServerCredentials is the base class name of the class that u put in the app_code folder

Hope this helps.

Regards,

Karen

|||

Thanks Karen. I am getting closer. I received the following error:

The path of the item '/Pages/ReportViewer.aspx?nsPortalReports.rptIssuesByRole&rs:Command=Render&intProjectID=119&RunBy=DanYeung' is not valid. The full path must be less than 260 characters long; other restrictions apply. If the report server is in native mode, the path must start with slash. (rsInvalidItemPath)

The report is located in the different project with the same solution. nsPortalReports is the project name and rptIssuesByRole is the report name. What is wrong in the path? Thanks.

DanYeung

|||

is ur report stored in the report server? and if ur reports are stored in the report server is there any kinda directory structure in it...

if u answer this questions may be i can give u a better answer.

Regards

Karen

|||

Yes, I deployed the reports to the report server. Thanks.

DanYeung

|||

are u able to run the reports using the report viewer...

Since ur reports are in the report server just give the path as i have given and it should work... if u need to pass any parameters to the report also let me know i will help u out..

Regards

Karen

|||

How do I run the report using ReportViewer? Yes, I have to pass parameters. So I am not supposed pass parameters in the ReportPath? Is my path correct?

ReportViewer1.ServerReport.ReportPath = "/Pages/ReportViewer.aspx?nsPortalReports.rptIssuesByRole&rs:Command=Render&intProjectID=" & cboProject.SelectedValue & "&RunBy=" & Session("strEmployeeName")

Thanks.

DanYeung

|||

your report Path doesnt Look right to me.

Instead of specifying the report Path... Specify a the ReportServerURL so that u can access ur report server directly. and the url will behttp://IpAddress/ReportServer.

take a look at this code as to how to specify the parameters and the report Path

ReportViewer1.ProcessingMode = ProcessingMode.Remote ReportViewer1.ShowCredentialPrompts =True ReportViewer1.ShowExportControls =False ReportViewer1.ServerReport.ReportServerCredentials =New ReportServerCredentials() ReportViewer1.ServerReport.ReportServerUrl =New Uri("http://IPAddress/ReportServer") s =CType(PreviousPage.FindControl("ddlReportType"), DropDownList).SelectedItem.ValueIf (s ="/Statements/StatementMain")Then ReportViewer1.ServerReport.ReportPath ="/Statements/StatementMain"Dim ClientIDAs String = Session("ClientId")Dim param(0)As Microsoft.Reporting.WebForms.ReportParameter'param(0) = New Microsoft.Reporting.WebForms.ReportParameter("PeriodId", "") param(0) =New Microsoft.Reporting.WebForms.ReportParameter("ClientId", ClientID,False) ReportViewer1.ServerReport.SetParameters(param)ReportViewer1.ServerReport.Refresh()End If
 
u can omit the parameters so that u can directly set them while u run the reports.Hope this helps.

Regards,

Karen

No comments:

Post a Comment