C# 请求失败,HTTP 状态为 401:未授权 IN SSRS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19247645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:30:24  来源:igfitidea点击:

The request failed with HTTP status 401: Unauthorized IN SSRS

c#sql-serverasp.net-mvc-3reporting-servicesssrs-2008

提问by Sam M

My Application is in Asp.Net MVC3coded in C#, i have a SSRS solution in SQL Server Business Intelligence Developement Studioin Visual Studio 2008, I'm calling the SSRS report through my Asp.Net MVC3application. My application was running fine a couple of days back but suddenly i'm getting an error as follows:

我的应用程序在 Asp.Net MVC3编码中C#,我有一个SSRS的解决方案SQL Server Business Intelligence Developement StudioVisual Studio 2008,我打电话,穿过我的SSRS报告Asp.Net MVC3应用。几天前我的应用程序运行良好,但突然我收到如下错误:

My Error :

我的错误:

The request failed with HTTP status 401: Unauthorized.

My Attempts

我的尝试

  1. My SSRS reports are deployed on my local server. I have my credential properly set in the datasource of my SSRS report solution.

  2. I tried to add the tag in my web.config <identity impersonate="true" userName="Domain\username" password="Password" />

  3. I tried adding IReportServerCredentials reportCredentials = new ReportServerCredentials("MyUserName", "MyPassword", "ServerName");

  4. I ran the Visual Studio as 'Run as Administrator'.

  5. I tried the solution mentioned in this link Creating a key using RegEdit

  6. UpdateI tried the following solution as well but the same result : Unauthorized error in SSRS

  1. 我的 SSRS 报告部署在我的local server. 我在 SSRS 报告解决方案的数据源中正确设置了我的凭据。

  2. 我试图在我的 web.config 中添加标签 <identity impersonate="true" userName="Domain\username" password="Password" />

  3. 我尝试添加 IReportServerCredentials reportCredentials = new ReportServerCredentials("MyUserName", "MyPassword", "ServerName");

  4. 我将 Visual Studio 作为'Run as Administrator'.

  5. 我尝试了此链接中提到的解决方案使用 RegEdit 创建密钥

  6. 更新我也尝试了以下解决方案,但结果相同:SSRS 中的未授权错误

None of the above solutions worked, but when i run the same solution on some other machine than the solutions works well and no error is displayed. Its only when i run the solution from my machine then i get the error The request failed with HTTP status 401: Unauthorized.

上述解决方案均无效,但是当我在其他机器上运行相同的解决方案时,这些解决方案运行良好且未显示任何错误。只有当我从我的机器上运行解决方案时才会出现错误The request failed with HTTP status 401: Unauthorized.

回答by vikky

Try this

尝试这个

public void GetConnectionOfReportServer()
        {
            try
            {
                NetworkCredential credential = new NetworkCredential("administrator", "password@123", "Domain");
                this.reportViewer1.ServerReport.ReportServerCredentials.NetworkCredentials = credential;

                //select where the report should be generated with the report viewer control or on the report server using the SSRS service.
                this.reportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local;
                this.reportViewer1.ServerReport.ReportServerUrl = new Uri(@"http://localhost/ReportServer");
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }

回答by user2959497

the request failed with http status 401 unauthorized. ssrs 2008

请求失败,http 状态 401 未经授权。ssrs 2008

from web.config add key="RS_UName" value="rajiv-pc" ---- incorrect

从 web.config 添加 key="RS_UName" value="rajiv-pc" ---- 不正确

replace above value with your system name (rajiv) not sql server name like rajiv-pc

用您的系统名称 (rajiv) 替换上面的值,而不是像 rajiv-pc 这样的 sql 服务器名称

always there is system name not sql server name.

总是有系统名称而不是 sql server 名称。

from web.config add key="RS_UName" value="rajiv" --- correct

从 web.config 添加 key="RS_UName" value="rajiv" --- 正确

回答by pedram

I am also getting the same error,

我也遇到同样的错误,

The request failed with HTTP status 401: Unauthorized.

Let me share what I tried and it is working fine now.

让我分享我的尝试,现在它运行良好。

public class CustomSSRSCredentials : IReportServerCredentials
    {
        private string _SSRSUserName;
        private string _SSRSPassWord;
        private string _DomainName;

        public CustomSSRSCredentials(string UserName, string PassWord, string DomainName)
        {
            _SSRSUserName = UserName;
            _SSRSPassWord = PassWord;
            _DomainName = DomainName;
        }

        public System.Security.Principal.WindowsIdentity ImpersonationUser
        {
            get { return null; }
        }

        public ICredentials NetworkCredentials
        {
            get { return new NetworkCredential(_SSRSUserName, _SSRSPassWord, _DomainName); }
        }

        public bool GetFormsCredentials(out Cookie authCookie, out string user,
         out string password, out string authority)
        {
            authCookie = null;
            user = password = authority = null;
            return false;
        }
    }

Inside page_loadevent,

内部page_load事件,

if (!Page.IsPostBack)
{
    ReportViewer1.ProcessingMode = ProcessingMode.Remote;
    IReportServerCredentials ssrscredentials = new CustomSSRSCredentials("MyUserName", "MyPassword", "ServerName");
    ServerReport serverReport = ReportViewer1.ServerReport;
    ReportViewer1.ServerReport.ReportServerCredentials = ssrscredentials;
    serverReport.ReportServerUrl = new Uri("ReportPathKey");
    serverReport.ReportPath = "/Reports/MyReport";
    serverReport.Refresh();
}

This worked for me!

这对我有用!

回答by AdrianBG

curiousguy's answer solved my problem. thanks man!

好奇人的回答解决了我的问题。谢啦!

Also, check your web.config. It must have must the following:

另外,请检查您的 web.config。它必须具备以下条件:

`

`

<system.web>
<httpHandlers>
      <add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"
        validate="false" />
    </httpHandlers>
</system.web>
<system.webServer>
<handlers>
      <add name="ReportViewerWebControlHandler" preCondition="integratedMode" verb="*" path="Reserved.ReportViewerWebControl.axd"
       type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, 
          PublicKeyToken=89845dcd8080cc91" />
</handlers>
  </system.webServer>

`

`

Public token must be the same otherwise it will blow another error.

公共令牌必须相同,否则会引发另一个错误。

回答by Caty Hespel

I had the same problem. Everything worked fine and then suddently I receive a 401 in the reportviewer iframe instead of my report in debug mode (localhost). I had been fiddling with settings to get it running on the live site so I thought I did something wrong...

我有同样的问题。一切正常,然后突然我在报告查看器 iframe 中收到一个 401,而不是我在调试模式(本地主机)下的报告。我一直在摆弄设置以使其在实时站点上运行,所以我认为我做错了什么......

Tried several things and then decided to just reboot and guess what??? My domain password had expired so authentication failed!!! Stupid stupid stupid me!

尝试了几件事,然后决定重新启动并猜猜是什么???我的域密码已过期,因此身份验证失败!!!笨笨笨笨笨笨的我!

Thought I'd add my solution here in case people run into the same stupid situation and loose precious time figuring out what they did wrong...

我想我会在这里添加我的解决方案,以防人们遇到同样愚蠢的情况并浪费宝贵的时间找出他们做错了什么......

回答by Makonor Daniel

Please just check the web service url you're pointing to. I deployed an SSRS on two separate server and one worked fine while the other threw this error "the request failed with http status 401 unauthorized."

请检查您指向的网络服务网址。我在两个单独的服务器上部署了一个 SSRS,一个运行良好,而另一个则抛出了这个错误“请求失败,http 状态 401 未经授权”。

So after a few hours I saw saw that I wasn't pointing to the web service url on my web.config. Example change on the web.config the report service url: http://weburl/ReportServer.

所以几个小时后,我看到我没有指向我的 web.config 上的 web 服务 URL。web.config 上的示例更改报告服务 URL:http://weburl/ReportServer

THIS WORKS!!!

这有效!!!