本文介绍了为 Reporting Services 启用 CORS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在 Reporting Services 中启用 CORS,以便我可以使用 ajax 从我的 Web 应用程序下载报告.到目前为止,我了解到的是,SSRS 不再使用 IIS,而是使用 http.sys 来为 web.requests 提供服务.是否有一种简单的方法可以向 SSRS(2012)添加 CORS 支持?
I need to enable CORS in Reporting Services so that I can download reports from my web application using ajax.What I've learned so far is, that SSRS is no longer using IIS, but http.sys to serve web.requests.Is there a simple way to add CORS support to SSRS (2012)?
推荐答案
我设法通过将以下代码添加到 reportserver 目录中的 global.asax 来使其工作.
I managed to get this working by adding the following code to the global.asax in reportserver directory.
<%@ Application Inherits="Microsoft.ReportingServices.WebServer.Global" %>
<%@ Import namespace="System.Web" %>
<%@ Import namespace="System.Security" %>
<script runat="server">
private static bool init;
private static object lockObject = new object();
void Application_BeginRequest(object sender, EventArgs e)
{
lock(lockObject)
{
if (!init)
{
HttpContext context = HttpContext.Current;
HttpResponse response = context.Response;
string allow = @"Access-Control-Allow-Origin";
// enable CORS
response.AddHeader(allow, "http://yoursourcedomain.com");
response.AddHeader(@"X-Frame-Options", "ALLOW-FROM http://yoursourcedomain.com");
response.AddHeader(@"Access-Control-Allow-Credentials", "true");
if (context.Request.HttpMethod == "OPTIONS")
{
response.AddHeader(@"Access-Control-Allow-Methods", "GET, POST");
response.AddHeader(@"Access-Control-Allow-Headers", "Content-Type, Accept, Authorization");
response.AddHeader(@"Access-Control-Max-Age", "1728000");
response.StatusCode = 200;
response.End();
HttpApplication httpApplication = (HttpApplication)sender;
httpApplication.CompleteRequest();
}
init = true;
}
else
{
init = false;
}
}
}
</script>
HTH干杯戴夫
这篇关于为 Reporting Services 启用 CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!