Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9188985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:08:35+00:00 2026-06-17T20:08:35+00:00

I have a simple ASP.NET MVC site with a single controller and some actions

  • 0

I have a simple ASP.NET MVC site with a single controller and some actions in it. A user needs to log in to access any action (Windows Authentication, ‘Authorize’ attribue on the controller class).

When accessing the site via a browser everything works fine.

On the client, I would also like to access the site via System.Net.WebClient. I set the credentials and all actions that return a View work just fine.

But when I try to access an action that tries to make a redirect (using Controller’s Rediret() method) the server returns a 401 Unauthorized.

I’ve read about problems with WebClient and Windows authentication but the stuff I found does not seem to apply here, because everything works except for the redirects.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:08:37+00:00Added an answer on June 17, 2026 at 8:08 pm

    So after some weeks I had some time to come back to this problem and as it turns out, it has nothing to do with ASP.NET at all and it’s WebClient’s fault.
    WebClient clears its credentials before following a redirect, so in this case the original request was successful but the error the occurred when WebClient tried to access the page it had been redirected to, because authentication is necessary for the target, too.

    Unfortunately, you cannot just toggle this behavior via a Property. So my solution looks like this.

    //Credentials necessary for the request(s)
    var cred = new NetworkCredential(username, password);
    
    //Create initial request
    var request = (HttpWebRequest)WebRequest.Create(url);
    request.AllowAutoRedirect = false;
    request.Credentials = cred;
    
    //Get response to first request
    var response = (HttpWebResponse)request.GetResponse();
    
    //Follow redirects
    while (response.StatusCode == HttpStatusCode.Redirect && maxRedirects > 0)
    {
        //Build new URI for redirect target
        var uri = response.ResponseUri;
        url = String.Format("{0}://{1}:{2}{3}", uri.Scheme, uri.Host, uri.Port, response.GetResponseHeader("Location"));    
    
        //Create new request
        request = (HttpWebRequest)WebRequest.Create(url);
        request.AllowAutoRedirect = false;
        request.Credentials = cred;
    
        //Get new response
        response = request.GetResponse() as HttpWebResponse;
    
        maxRedirects--;
    }
    
    //Get the response's content
    var sReader = new StreamReader(response.GetResponseStream());
    string responseStr = sReader.ReadToEnd();
    

    Instead of using WebClient, I’m using HttpWebRequest and HttpWebResponse and follow the Redirect manually. Before making the request, I set the credentials necessary to complete the request

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple ASP.NET MVC 3 site, from my Controller out of the
I have a simple form on an ASP.NET MVC site that I'm building. This
in asp.net mvc, if u create a simple site, some security code will be
I have a very simple CRUD asp.net-mvc site that uses nhibernate to interface with
I have a simple ASP.NET MVC 1.0 site w/ Home, Upload, and About links
I have a very simple ASP.NET MVC site which displays images from the database.
I have put together a small ASP.NET MVC 2 site that does some very
I have a pretty simple ASP.NET MVC Site Application. (MVC 1.0) I have NO
I have simple jQuery Mobile site created using asp.net mvc 2 and uses basic
I created a simple ASP.NET MVC version 1.0 application. I have a ProductController which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.