Sqlserver
 sql >> Base de données >  >> RDS >> Sqlserver

La demande a échoué avec le statut HTTP 401 :non autorisé dans SSRS

Je reçois également la même erreur,

The request failed with HTTP status 401: Unauthorized.

Permettez-moi de partager ce que j'ai essayé et cela fonctionne bien maintenant.

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

À l'intérieur de page_load événement,

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

Cela a fonctionné pour moi !