views:

314

answers:

0

I am trying to execute a report as a pdf in asp.net c#. I have everything working great, except for the sqlauth cookie. If I use an iframe and render a dummy report, then use that sqlAuthCookie cookie (set by report server) in the code-behind, it works, so I know my syntax and method , etc is correct. What I need to do is add the sqlauth cookie to the ReportExecution Service cookie Container, or some other method of passing credentials. I am using Forms Authentication. I am using a, ashx handler with a jquery ajax call to execute the report. Here is my code behind page.

  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");

        }
    }

You can see where the rs.CookieContainer.Add(new Cookie("sqlAuthCookie" is and that is how I can make it work. Please please please Help!!! Thanks all!