Passing Credentials to Sql Report Server 2008 using C#

I am running my own asp.net application using VS2010. And report server is in a different machine. Now to run the report from server, it was not easy as SSRS 2008 does not allow anonymous login. So, I tried to pass Credentials to SSRS which will be stored inside my web page so that users will be able to see the report without logging in. Here are the code:

using System; 
using System.Configuration; 
using System.Data; using System.Linq; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Xml.Linq; 
using System.Net; // You must need this reference.
using Microsoft.Reporting.WebForms;  

public partial class _Default : System.Web.UI.Page  
{     protected void Page_Load(object sender, EventArgs e)     
 {    
if (!this.IsPostBack)      // If you do not use this condition, the page will auto refresh and all parameters are reset to default or null values. 
{     
ReportViewer1.Width = 800;        
ReportViewer1.Height = 600;         
ReportViewer1.ProcessingMode = ProcessingMode.Remote;        
IReportServerCredentials irsc =new CustomReportCredentials("MYdomainusername", "MYpassworw", "domain_name");         
ReportViewer1.ServerReport.ReportServerCredentials = irsc;        
ReportViewer1.ServerReport.ReportServerUrl = new Uri("http://192.168.0.1/ReportServer/");         
ReportViewer1.ServerReport.ReportPath = "/autonarudzba/listanarudzbi";       
ReportViewer1.ServerReport.Refresh();    
} 
}
} 

reff: http://stackoverflow.com/questions/671694/passing-credentials-to-sql-report-server-2008

public class CustomReportCredentials : IReportServerCredentials 
{      private string _UserName;      
private string _PassWord;      
private string _DomainName;      
public CustomReportCredentials(string UserName, string PassWord, string DomainName)      
{         _UserName = UserName;         _PassWord = PassWord;         _DomainName = DomainName;      }      
public System.Security.Principal.WindowsIdentity ImpersonationUser      {          get { return null; }       }       
public ICredentials NetworkCredentials      {         get { return new NetworkCredential(_UserName, _PassWord, _DomainName); }      }      
public bool GetFormsCredentials(out Cookie authCookie, out string user,       out string password, out string authority)      {         authCookie = null;         user = password = authority = null;         return false;      } 
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s