[置顶] ReportingService WebService Form身份验证

这个验证的前提是你的报表服务器的身份验证已经改成了Form验证了不再是Windows身份验证了。 然后你再调用ReportingService的服务的时候因为默认还是windows验证,这个时候需要做一些调整。

在MSDN上面有提到过:
Reporting Services Web service 提供自定义身份验证,以便 Report Manager 和报告服务器能够进行窗体身份验证。
Reporting Services Web service 的 LogonUser 方法用于将凭据提交给报告服务器,以进行身份验证。Web service 使用 HTTP 标头将身份验证票据(称为“Cookie”)从服务器传递到客户端,以响应已验证的登录请求。

身份验证进程如下:

  1. 客户端应用程序调用 Web service 方法 LogonUser 对用户进行身份验证。
  2. Web service 调用安全扩展(具体而言,是指实现 IAuthenticationExtension 的类)的 LogonUser 方法。
  3. LogonUser 的实现验证用户存储或安全机构中的用户名和密码。
  4. 身份验证成功后,Web service 将创建 Cookie 并针对会话对其进行管理。
  5. Web service 通过 HTTP 标头将身份验证票据返回给调用的应用程序。

Web service 通过安全扩展成功对用户进行身份验证后,将生成一个 Cookie,用于随后的请求。由于报告服务器没有安全机构,因此该 Cookie 不会一直保存在自定义安全机构中。Cookie 从 Web service 方法 LogonUser 返回,并用于随后的 Web service 方法调用以及 URL 访问。

具体使用:

将 Web Service 用于自定义安全验证,可以将 Web service API 用于窗体身份验证,就像将 Windows Authentication 用于窗体身份验证一样。不过,必须调用 Web service 代码中的 LogonUser,并传递当前用户的凭据。此外,Web service 客户端将不具备 Internet Explorer 或其他 Web 浏览器提供的自动 Cookie 管理功能。必须扩展 ReportingService 代理类以包含 Cookie 管理。为此,可以覆盖 Web service 类的 GetWebRequest 和 GetWebResponse 方法。

创建一个项目,然后引用ReportService2005.asmx的web服务。

protected void Page_Load(object sender, EventArgs e)

{

RenderReport report = new RenderReport();

byte [] result = report.RenderReportToPDF();

//To display the PDF in web browser, set the right content type.

Response.ContentType = “Application/pdf”;

 //Write the byte array to the default output stream.

Response.OutputStream.Write(result, 0, result.Length);

 //Flush the contents to be displayed in the browser.

Response.Flush();

}



//Clash to take care of report rendering

 public class RenderReport : ReportingService2005

{

 //Change to point to your report.

 private string ReportPath = “/Test”;

 private string m_authCookieName;

 private Cookie m_authCookie;

 public RenderReport()

{



 // Set the server URL. You can pull this from a config file or what ever way you want to make it dynamic.

 base.Url = http://kaneco1/reportserver2008/reportservice2005.asmx;



 // Calling the LogonUser method defined in the ReportService2005.asmx end point.

 // The LogonUser method authenticates the specified user to the Report Server Web Service when custom authentication has been configured.

 // This is to authenticate against the FBA code and then store the cookie for future reference.

 try

{

 base.LogonUser(“FBA username”, “password”, null);

}

 catch (Exception

)

{

}

}



 /// <summary>

 /// Overriding the method defined in the base class.

 /// </summary>

 /// <param name=”uri”></param>

 /// <returns></returns>

 protected override WebRequest GetWebRequest(Uri uri)

{

 HttpWebRequest request;

request = (HttpWebRequest)HttpWebRequest .Create(uri);

request.Credentials = base .Credentials;

request.CookieContainer = new CookieContainer ();



if (m_authCookie != null

)

{

request.CookieContainer.Add(m_authCookie);

}





return request;

}



 /// <summary>

 /// Overriding the method defined in the base class.

 /// </summary>

 /// <param name=”request”></param>

 /// <returns></returns>

 protected override WebResponse GetWebResponse(WebRequest request)

{

 WebResponse response = base .GetWebResponse(request);

 string cookieName = response.Headers[“RSAuthenticationHeader”];

 if (cookieName != null)

{

m_authCookieName = cookieName;

 HttpWebResponse webResponse = (HttpWebResponse )response;

 Cookie authCookie = webResponse.Cookies[cookieName];

 // save it for future reference and use.

m_authCookie = authCookie;

}

 return response;

}



/// <summary>

 ///Simplified implementation from http://msdn.microsoft.com/en-us/library/reportexecution2005.reportexecutionservice.render.aspx

 /// Additionaly it is autheticating against the custom security extension. In our case it is FBA.

 /// </summary>

 /// <returns>Byte array containing the rendered report in PDF format </returns>



 public byte [] RenderReportToPDF()

{



//Create a proxy object for ReportExecution2005.asmx referenced in the project.

 //You can pull this from a config file or what ever way you want to make it dynamic.

 ReportExecutionService rs = new ReportExecutionService ();

rs.Url = “http://kaneco1/ReportServer2008/ReportExecution2005.asmx”;

 //Simplified code from http://msdn.microsoft.com/en-us/library/reportexecution2005.reportexecutionservice.render.aspx

 byte[] result = null;  

 string reportPath = “/SimpleSelect” ;

 string format = “PDF” 

string historyID = null;

 string devInfo = “”;

 string encoding;

 string mimeType;

 string extension;



//Since warning is definied in both ReportService2005 and ReportExecution2005 endpoints,

 //qualifying it with the appropriate namespace.

WebServiceFBARenderMethod.RE2K5.Warning[] warnings = null;

 string[] streamIDs = null;

 //Attaching the cookie received from the LogonUser method in the constructor.

 //Only when autheticated, it will proceed further with ReportExecution2005.asmx end point calls.

 if (null != m_authCookie)

{



//Store the cookie in the cookie container within ReportExecutionService object.

//So any subsequent call will make use of this authenticated cookie and will be succeeding.

rs.CookieContainer = new CookieContainer();

rs.CookieContainer.Add(m_authCookie);



ExecutionInfo execInfo = new ExecutionInfo();

ExecutionHeader execHeader = new ExecutionHeader();



rs.ExecutionHeaderValue = execHeader;

execInfo = rs.LoadReport(reportPath, historyID);



try

{

result = rs.Render(format, devInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);

}

catch (SoapException e)

{

}

}

else

{

//Logic to recall the LogonUser code with proper username / password.

}



//Byte array containing the rendered report in PDF.

return result;

你可能感兴趣的:(report)