SSRS 报告查看器 - 401 未经授权

SSRS Reportviewer - 401 Unauthorized

我们正在将 SQL 服务器报告从 SQL Server 2008 (SP3) 机器移动到 SQL 2012 (SP1) 机器。报表 运行 在报表管理器中正常(即 https://SQLRep2012/Reports

但是,ASP.net 申请 returns "The request failed with HTTP status 401: Unauthorized."

我知道用户有权限,因为我正在使用具有所有 network/SQL 服务器权限的管理员帐户对其进行测试。

找到下面的代码...

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Security.Principal;
using System.Net;

public partial class Reports_ReportViewer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            TimeReport GetReport1;
            GetReport1 = (TimeReport)Session["Report"];
            string FileName = GetReport1.GetReportFileName();

            //ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote;
            ReportViewer1.ServerReport.ReportServerUrl = new Uri("https://SQLRep2012/ReportServer");
            ReportViewer1.ServerReport.ReportPath = "/TestReports/" + FileName;

            if (GetReport1.ParameterCount() != 0)
            {
                Microsoft.Reporting.WebForms.ReportParameter[] parameters1 = new Microsoft.Reporting.WebForms.ReportParameter[GetReport1.ParameterCount()];

                for (int i = 0; i < GetReport1.ParameterCount(); i++)
                {
                    parameters1[i] = new Microsoft.Reporting.WebForms.ReportParameter(GetReport1.ParameterName(i), GetReport1.ParameterValue(i));
                }

                //Create the creditial object and assign the username and password.
                ReportViewerCredentials rvc = new ReportViewerCredentials(ConfigurationManager.AppSettings["ReportUserID"],
                    ConfigurationManager.AppSettings["ReportUserPwd"],
                    ConfigurationManager.AppSettings["ReportUserDomain"]);

                //Make sure the credential object has the same report server url.
                rvc.ReportServerUrl = ReportViewer1.ServerReport.ReportServerUrl;

                //Save the credentials to the ReportViewer object.
                ReportViewer1.ServerReport.ReportServerCredentials = rvc;

                ReportViewer1.ServerReport.SetParameters(parameters1);
            }
        }       
    }
}
public class ReportViewerCredentials : Microsoft.Reporting.WebForms.IReportServerCredentials
{
    private string _username;
    private string _password;
    private string _domain;

    public Uri ReportServerUrl;

    public ReportViewerCredentials(string username, string password, string domain)
    {
        _username = username;
        _password = password;
        _domain = domain;
    }

    #region IReportServerCredentials Members

    public System.Security.Principal.WindowsIdentity ImpersonationUser
    {
        get
        {
            return null;
        }
    }
    public System.Net.ICredentials NetworkCredentials
    {
        get
        {
            System.Net.NetworkCredential nc = new NetworkCredential(_username, _password, _domain);
            CredentialCache cc = new CredentialCache();
            cc.Add(ReportServerUrl, "Negotiate", nc);
            return cc;
        }
    }
    public bool GetFormsCredentials
        (out Cookie authCookie,
        out string username,
        out string password,
        out string authority)
    {
        authCookie = null;
        authority = null;
        password = null;
        username = null;
        return false;
    }

    #endregion
}

在我发布上述问题之前,我已经想出了答案。麻烦来自网络凭据。以前我有这个....

public System.Net.ICredentials NetworkCredentials
{
    get
    {
        System.Net.NetworkCredential nc = new NetworkCredential(_username, _password, _domain);
        CredentialCache cc = new CredentialCache();
        cc.Add(ReportServerUrl, "Negotiate", nc);
        return cc;
    }
}

但是,这对 SQL 2012 年的我不起作用。我不得不将其更改为这个。

public System.Net.ICredentials NetworkCredentials
{
    get
    {            
        return new NetworkCredential(_username, _password, _domain);
    }
}

创建没有域参数的 NetworkCredential 对我有用。

public System.Net.ICredentials NetworkCredentials
{
    get
    {            
        return new NetworkCredential(_username, _password);
    }
}