【问题标题】:Enable CORS for Reporting Services为 Reporting Services 启用 CORS
【发布时间】:2023-03-13 12:26:01
【问题描述】:

我需要在 Reporting Services 中启用 CORS,以便我可以使用 ajax 从我的 Web 应用程序下载报告。 到目前为止我了解到的是,SSRS 不再使用 IIS,而是使用 http.sys 来服务 web.requests。 有没有一种简单的方法可以将 CORS 支持添加到 SSRS (2012)?

【问题讨论】:

    标签: reporting-services http.sys


    【解决方案1】:

    我设法通过将以下代码添加到报告服务器目录中的 global.asax 来实现此功能。

    <%@ 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 干杯 戴夫

    【讨论】:

    • 这里有没有你没有提到的步骤?它似乎不起作用?
    【解决方案2】:

    对于 SSRS 2017(Restful API) 使用 SSMS 连接到报告服务器,并确保在连接对话框中将服务类型设置为 Reporting Services。 此外,如果您使用不同的身份验证方法,请设置并输入您的用户名/密码

    连接后,在头节点“服务器”上单击鼠标右键-> 属性-> 高级 你会发现所有的CORS属性,设置你需要的

    https://docs.microsoft.com/en-us/sql/reporting-services/tools/server-properties-advanced-page-reporting-services?view=sql-server-2017

    【讨论】:

      【解决方案3】:

      去年一个类似的问题引起了微软员工"that's not possible, use ReportViewer control"的回答。

      【讨论】:

      • 使用reportViewer 控件不是一种选择,因为它既慢又笨重。我实际上只需要检索报告的 excel 导出,并且报告服务器 API 将是完美的..
      • 您的需求是可以理解的,但从 MS 员工所说的情况来看,可能无法通过 CORS 路由,因为无处可配置它。如果您需要检索 excel 导出,似乎您可以在检索服务器端执行然后传递结果。
      • 在服务器端做这件事是一种选择,但挑战是让身份验证正常工作。我们正在使用集成身份验证。我已经设法通过将自定义 httpmodule 添加到报告服务器 web.config 来设置 CORS 标头。如果我现在只能说服 ajax 传递 Windows 身份验证令牌,我可能会有一个解决方案。
      【解决方案4】:

      re:et3rnal 的正确答案(有很多方法,但这可能是最直接的,推荐):

      如前所述,您可以在此处控制请求标头和 CORS 设置。

      【讨论】:

        猜你喜欢
        • 2019-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-20
        • 2010-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多