简体   繁体   中英

Request failed with HTTP status 401: Unauthorized on second try

I have an SSRS report and I am calling this report on my web project. My client side page like below image:

网站

My .cs page code is:

public reports()
{
    Init += Page_Init;
    Load += Page_Load;
}

protected void Page_Init(object sender, System.EventArgs e)
{
    ReportViewer1.ServerReport.ReportServerCredentials = new MyReportServerCredentials();
}

[Serializable()]
public sealed class MyReportServerCredentials : IReportServerCredentials
{
    public string UserName = ConfigurationManager.AppSettings["rvUser"];
    public string Password = ConfigurationManager.AppSettings["rvPassword"];

    public string Domain = ConfigurationManager.AppSettings["rvDomain"];
    public WindowsIdentity ImpersonationUser
    {
        //Use the default windows user.  Credentials will be
        //provided by the NetworkCredentials property.

        get { return null; }
    }

    public ICredentials NetworkCredentials
    {
        get
        {
            //Read the user information from the web.config file. 
            //By reading the information on demand instead of storing
            //it, the credentials will not be stored in session,
            //reducing the vulnerable surface area to the web.config
            //file, which can be secured with an ACL.

            if ((string.IsNullOrEmpty(UserName)))
            {
                throw new Exception("Missing user name from web.config file");
            }

            if ((string.IsNullOrEmpty(Password)))
            {
                throw new Exception("Missing password from web.config file");
            }

            if ((string.IsNullOrEmpty(Domain)))
            {
                throw new Exception("Missing domain from web.config file");
            }

            return new NetworkCredential(UserName, Password, Domain);
        }
    }

    public bool GetFormsCredentials(out Cookie authCookie,
              out string userName, out string password,
              out string authority)
    {
        authCookie = null;
         userName = UserName;
         password = Password;
         authority = Domain;
        // Not using form credentials
        return false;
    }



    private void ShowReport()
    {
        ReportViewer1.ProcessingMode = ProcessingMode.Remote;
        ReportViewer1.ServerReport.ReportServerUrl = new Uri(reportServer);
        ReportViewer1.ServerReport.ReportPath = "/xyz/ReportNewname";
        ReportParameter[] param = new ReportParameter[8];
        param[0] = new ReportParameter("p_ClientID", ClientID);
        param[1] = new ReportParameter("p_CarrierID", carrierId);
        param[2] = new ReportParameter("p_StartDate", BeginDate);
        param[3] = new ReportParameter("p_EndDate", EndDate);
        param[4] = new ReportParameter("p_ClaimSubTypeID", ClaimTypeID);
        param[5] = new ReportParameter("p_SalesAuditorId", SalesAuditorID);
        param[6] = new ReportParameter("p_IsPaid", PaidClaim);
        param[7] = new ReportParameter("p_IsOpen", OpenClaim);
        ReportViewer1.ServerReport.SetParameters(param);
}

My question is: 1st time when I click on left side treeview on GUI (Claims(Paid)) item and enter begin date & end date. Report works fine. Now, suppose after this I click on any other item like- Claim(ALL) and after that again I click on (Claims(Paid)) and give same date parameters it's show me the following error:

The request failed with HTTP status 401: Unauthorized.

If I will deploy it on IIS and try to run it, it show me this error:

If you have reached this page, there was an error that caused AFS Claims to show it. While we make every effort to ensure your user experience is without error, there are times when the errors encountered may not have been something we know about.

Please select back button in the browser and review the last page you were trying to use.

Please Note: For security purposes, there is a session time limit on this web site for users that are logged in, even if you have selected the checkbox to remember your login. If you have exceeded the timeout for the session, it is quite possible that the action you are trying to complete will not work.

Can anybody help me? Why does it work 1st time, but not the second time?

Try placing the set credentials portion in your ShowReport() method instead of in the page init method. Set a breakpoint to see what the Credentials look like on your second request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM