为SSRS执行服务设置sqlAuthCookie

时间:2009-07-24 20:48:17

标签: c# asp.net reporting-services httphandler

我正在pdf中以asp.net c#身份执行报告。除了sqlauth cookie之外,我的一切都很好。如果我使用iframe并呈现虚拟报告,那么在代码隐藏中使用sqlAuthCookie cookie(由报表服务器设置),它可以工作,所以我知道我的语法和方法等是正确的。我需要做的是将sqlauth cookie添加到ReportExecution Service cookie容器或其他一些传递凭据的方法。我正在使用Forms Authentication。我正在使用带有ashx调用的jquery ajax处理程序来执行报告。这是我的代码页面。

  public void ProcessRequest(HttpContext context)
    {
        // 8863443a-06d6-4aeb-8fa8-43be545da1a3
        var rs = new ReportExecutionService();
        rs.Credentials = CredentialCache.DefaultCredentials;
        //rs.Credentials = new NetworkCredential("zachariah.curtis", "password", "AGLO");
        rs.Url = ConfigurationManager.AppSettings["ReportExecutionURL"];

        NetworkCredential creds = rs.Credentials.GetCredential(context.Request.Url, context.User.Identity.AuthenticationType);
        rs.LogonUser(creds.UserName, creds.Password, null);

        var cookies = context.Request.Cookies.AllKeys.ToList();
        var cookies3 = rs.CookieContainer.GetCookies(new Uri(rs.Url)); 
        var sqlauth = context.Request.Cookies["sqlAuthCookie"];
        rs.CookieContainer = new CookieContainer();
        cookies.ForEach(delegate(String item)
        {
            rs.CookieContainer.Add(new Cookie(context.Request.Cookies[item].Name, context.Request.Cookies[item].Value, context.Request.Cookies[item].Path, context.Request.Cookies[item].Domain ?? ".usda.gov"));
            rs.CookieContainer.Add(new Cookie("sqlAuthCookie", "A78B8A2D714BF058DF7258927717A4D34FA9F8A0BA54FCCB006543DEBF755D2670636E172675107A9FCAF77F66A0E7E2EE4321464602C7E61138C224B8612B05E12DC1DFF878E8748CE267FF19839198", "/", "iasdev.dev.sc.egov.usda.gov"));
        });


        // Render arguments
        byte[] result = null;
        // Make sure you use your correct org database name of the following line
        string reportPath = "/ProTractsCMT/TestReport";
        var contractId = context.Request["ContractId"];
        string format = "PDF";
        string historyID = null;
        string devInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";

        // Prepare report parameter.

        ParameterValue[] parameters = new ParameterValue[1];
        parameters[0] = new ParameterValue();
        parameters[0].Name = "@CONTRACTID";
        parameters[0].Value = contractId;

        DataSourceCredentials[] credentials = null;
        string showHideToggle = null;
        string encoding;
        string mimeType;
        string extension;
        Warning[] warnings = null;
        ParameterValue[] reportHistoryParameters = null;
        string[] streamIDs = null;

        ExecutionInfo execInfo = new ExecutionInfo();
        ExecutionHeader execHeader = new ExecutionHeader();

        rs.ExecutionHeaderValue = execHeader;

        execInfo = rs.LoadReport(reportPath, historyID);

        //rs.SetExecutionParameters(parameters, "en-us");
        string SessionId = rs.ExecutionHeaderValue.ExecutionID;


        Console.WriteLine("SessionID: {0}", rs.ExecutionHeaderValue.ExecutionID);

        try
        {
            result = rs.Render(format, devInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);
            execInfo = rs.GetExecutionInfo();
            Console.WriteLine("Execution date and time: {0}", execInfo.ExecutionDateTime);
        }

        catch (SoapException err)
        {
            Console.WriteLine(err.Detail.OuterXml);
        }
        context.Response.ContentType = "text/plain";
        // save rendered pdf to db
        if (new calculations().SaveReport(result))
        {
            context.Response.Write("true");

        }
        else
        {
            context.Response.Write("true");

        }
    }

您可以看到rs.CookieContainer.Add(new Cookie("sqlAuthCookie"的位置,这就是我如何使其发挥作用。请帮助!!!谢谢大家!

1 个答案:

答案 0 :(得分:0)

您的设计是否要求您将当前登录用户的凭据/身份验证数据传递给SSRS?那么,是否允许该用户直接使用该特定凭证执行报告?

通常情况下,我宁愿假设您已经按照您明显尝试过的方式访问并执行了一些预定义用户的RS执行引擎:

 //rs.Credentials = new NetworkCredential("zachariah.curtis", "password", "AGLO");

您可以在应用设置中配置这些凭据。