SSRS Reportviewer - 401 Unauthorized

孤街醉人 提交于 2019-12-24 01:47:26

问题


We are moving SQL Server Reports from a SQL Server 2008 (SP3) machine to a SQL 2012 (SP1) machine. The reports run fine in report manager (ie https://SQLRep2012/Reports)

However, the ASP.net application returns "The request failed with HTTP status 401: Unauthorized."

I know the user has rights because I am testing it with an admin account with all network/SQL server rights.

Find the code below...

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
}

回答1:


I already figured out the answer before I posted the above question. The troubles came from the network credentials. Previously I had this....

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

However, this does not work for me on SQL 2012. I had to change it to just this.

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



回答2:


Create NetworkCredential without the domain parameter work for me.

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


来源:https://stackoverflow.com/questions/28501959/ssrs-reportviewer-401-unauthorized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!