SSRS Reportviewer-401未经授权

问题描述 投票:2回答:2

我们正在将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
}
asp.net ssrs-2008 ssrs-2012
2个回答
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);
    }
}

0
投票

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

public System.Net.ICredentials NetworkCredentials
{
    get
    {            
        return new NetworkCredential(_username, _password);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.