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

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

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.

Need Your Help

Why does this multiplication integer overflow result in zero?

java integer-overflow

After answering this question, I was confused about why the overflowing integer in this code resulted in 0 instead of a negative number. It's strange, why such an exact number? Why 0?

How can I get more information on an invalid DOM element through the Validator?

java xml xml-validation

I am validating an in-memory DOM object using the javax.xml.validation.Validator class against an XSD schema. I am getting a SAXParseException being thrown during the validation whenever there is s...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.