2012-09-13 29 views
1

我使用這個code來創建和SSRS下載PDF格式的報告,但我收到以下錯誤:如何在運行SQL Server Reporting Service時解決「遠程服務器返回錯誤:(401)未授權」?

The remote server returned an error: (401) Unauthorized.

我的代碼是:

protected void Page_Load(object sender, EventArgs e) 
{ 
    MemoryStream ms; 
    // this name is what the user will see when they are prompted for download. 
    string customFileName = "NewFileName.pdf"; 

    if (!this.IsPostBack) 
    { 
     try 
     { 
      string strRequest = "http://myServer.com/ReportServer?%2fmetadata_report%2fMetadataReport1&rs%3aCommand=Render&rs%3AFormat=PDF&grpId=3"; 
      WebRequest request = WebRequest.Create(strRequest); 
      request.ContentType = "application/pdf"; 

      string userName = "username"; 
      string password = "password"; 
      string domain = "myServer.com"; 

      // Create and then pass in network credentials to acecss the reporting server 
      System.Net.NetworkCredential credentials = new NetworkCredential(userName, password, domain); 
      request.Credentials = credentials; 

      HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

      using (response) 
      { 
       // Get the stream from the server 
       using (Stream stream = response.GetResponseStream()) 
       { 
        // Use the ReadFully method from the link above: 
        byte[] data = ReadFully(stream, response.ContentLength); 

        // Return the memory stream. 
        ms = new MemoryStream(data); 
       } 
      } 

      // Clear out the headers that may already exist, then set content type and add a header to name the file 
      Response.Clear(); 
      Response.ContentType = "application/pdf"; 
      Response.AddHeader("content-disposition", "inline; "); 

      //// Write the memory stream containing the pdf file directly to the Response object that gets sent to the client 
      ms.WriteTo(Response.OutputStream); 
     } 
    } 
} 

任何幫助將非常感激。

回答

1
+0

嗨內森,我認爲你是對的;這是一個許可問題。我在想我需要自己設置報表服務器配置。我開發了代碼[這裏](http://msdn.microsoft.com/en-us/library/microsoft.reporting.webforms.ireportservercredentials(v = vs80).aspx),但它仍然給我_ 「請求失敗,HTTP狀態401:未經授權」_錯誤。任何建議?謝謝。 – Fred

0

你可以試試這個?

// Create and then pass in network credentials to acecss the reporting server 
CredentialCache cc = new CredentialCache(); 
cc.Add(new Uri("http://myServer.com/"), "Negotiate", new NetworkCredential("Username", "Password", "Domain")); 
request.Credentials = cc; 
+0

嗨,我更新了我的代碼,但仍然得到相同的錯誤。你不認爲我的報表服務器配置中有什麼東西(例如認證)導致了這個問題?任何建議?謝謝。 – Fred

+0

您是否嘗試過使用該帳戶打開報告? –

+0

是的。我可以使用相同的帳戶(管理員)打開報告。 – Fred

0

因爲這個原因,在過去的一週中我失去了很多頭髮。在我的情況下,報告定義與我的Sql服務器和IIS位於同一臺服務器上。我通過從ssrs服務器提取報告來測試我的開發機器上的所有內容。在這一點上,一切都工作得很好。現在,只要我在生產服務器上部署我的應用程序,它就開始給我那個錯誤。我有一個單獨的報告用戶,使用開發機器的必要權限進行分類。微軟的安全功能來保護雙跳。如果你谷歌LSA米確定你會得到足夠的。銘記impersonate = true是不會做任何好事的。

以下是我的解決方案,它的工作就像一個魅力:

Method 2: Disable the authentication loopback checkRe-enable the behavior that exists in Windows Server 2003 by setting the DisableLoopbackCheck registry entry in the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa registry subkey to 1. To set the DisableLoopbackCheck registry entry to 1, follow these steps on the client computer:  Click Start, click Run, type regedit, and then click OK. Locate and then click the following registry subkey: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\LsaRight-click Lsa, point to New, and then click DWORD Value.Type DisableLoopbackCheck, and then press ENTER.Right-click DisableLoopbackCheck, and then click Modify.In the Value data box, type 1, and then click OK.Exit Registry Editor.Restart the computer.Note You must restart the server for this change to take effect. By default, loopback check functionality is turned on in Windows Server 2003 SP1, and the DisableLoopbackCheck registry entry is set to 0 (zero). The security is reduced when you disable the authentication loopback check, and you open the Windows Server 2003 server for man-in-the-middle (MITM) attacks on NTLM.

對於詳細研究:read here

希望它可以幫助別人。

謝謝

相關問題