SSRS Reportviewer - 401未经授权

时间:2015-02-13 14:38:39

标签: asp.net ssrs-2008 ssrs-2012

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

然而,ASP.net应用程序返回"请求失败,HTTP状态为401:未经授权。"

我知道用户有权利,因为我正在使用具有所有网络/ 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
}

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

在不使用domain参数的情况下创建NetworkCredential。

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