Thursday, August 28, 2008





The request failed with HTTP status 404: Not Found.





I was stuck on this issue for a week till I eventually found the solution and it was in bits and peaces all over the net. But I thought someone might benefit from getting the solution as a whole. Again I haven’t spent enough time to understand the solution but it worked and for now I have to be satisfied with that.







My hosting provider has reporting services installed on a separate machine to the actual website so this should work for most hosting setups. I wanted to embed the reports in my webpage using report viewer. The trick was trying to pass authentication information from my website to the reporting services.








To do this you need to create a ReportServerConnection class and register it in your web.config, along with this you need to add application key/value pairs to pass your report server credentials.






Web Config






Add the following code to a blank page and run then run the code to get the PublicKeyToken






Response.Write("
" +typeof(SportsDB.BLL.SimpleReportServerConnection).AssemblyQualifiedName);





Add the following in your webconfig. It goes under the assemblies tag.




<buildProviders>

<add extension=".rdlc" type="Microsoft.Reporting.RdlBuildProvider, Microsoft.ReportViewer.Common, Version=8.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx" />

buildProviders>





<add key="MyReportServerUrl" value="https://XXXX"/>

<add key="MyReportViewerUser" value="XXX"/>

<add key="MyReportViewerPassword" value="XXX"/>

<add key="MyReportViewerDomain" value="XXX"/>





As for the code here is the full class – (Don’t you just luv to cut and paste!!)





using System;

using System.Collections.Generic;

using System.Data;

using System.Configuration;

using System.Net;

using System.Security.Principal;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using Microsoft.Reporting.WebForms;





namespace XXXX

{

public interface IReportServerConnection2 : IReportServerConnection, IReportServerCredentials { }



public sealed class SimpleReportServerConnection : IReportServerConnection2

{



public WindowsIdentity ImpersonationUser

{

get

{

// Use the default Windows user. Credentials will be

// provided by the NetworkCredentials property.

return null;

}

}



public ICredentials NetworkCredentials

{

get

{

// Read the user information from the web.config file.

// By reading the information on demand instead of

// storing it, the credentials will not be stored in

// session, reducing the vulnerable surface area to the

// web.config file, which can be secured with an ACL.



// User name

string userName =

ConfigurationManager.AppSettings

["MyReportViewerUser"];



if (string.IsNullOrEmpty(userName))

throw new Exception(

"Missing user name from Web.config file");



// Password

string password =

ConfigurationManager.AppSettings

["MyReportViewerPassword"];



if (string.IsNullOrEmpty(password))

throw new Exception(

"Missing password from Web.config file");



// Domain

string domain =

ConfigurationManager.AppSettings

["MyReportViewerDomain"];



if (string.IsNullOrEmpty(domain))

throw new Exception(

"Missing domain from Web.config file");



return new NetworkCredential(userName, password, domain);

}

}



public bool GetFormsCredentials(out Cookie authCookie,

out string userName, out string password,

out string authority)

{

authCookie = null;

userName = null;

password = null;

authority = null;



// Not using form credentials

return false;

}



public Uri ReportServerUrl

{

get

{

string url =

ConfigurationManager.AppSettings[

"MyReportServerUrl"];



if (string.IsNullOrEmpty(url))

throw new Exception(

"Missing url from the Web.config file");



return new Uri(url);

}

}



public int Timeout

{

get

{

return 60000; // 60 seconds

}

}



public IEnumerable<Cookie> Cookies

{

get

{

// No custom cookies

return null;

}

}



public IEnumerable<string> Headers

{

get

{

// No custom headers

return null;

}

}



}

}

No comments: