15

I have an ASP.NET application where I'm trying to output the previously-visited local aspx page to html (its a report and I want to store a static html copy of it as an archive). I store the uri of the local page with:

Session["SummaryURI"] = Request.Url.AbsoluteUri;

and then in the next page I retrieve it with:

string url = Session["SummaryURI"].ToString();

url = url.Replace("static=false", "static=true");
//MessageLabel.Text = url;

//CREATE THE NEW FILE
WebRequest req = WebRequest.Create(url);
WebResponse res = req.GetResponse();

The part req.GetResponse() is where I'm getting my error (401 Unauthorized).

Do I need to configure something in IIS to allow this?

Do I need to edit file permissions or something?

Thanks for your help

By the way this works fine on my local IIS but not on my test server.

Daniel
  • 2,762
  • 3
  • 25
  • 24
  • 1
    Check this out http://social.technet.microsoft.com/Forums/en/winserversecurity/thread/c9239a89-fbee-4adc-b72f-7a6a9648331f – Aristos Apr 06 '11 at 20:46

4 Answers4

30

If you cannot enable Anonymous Authentication, try adding this to your WebRequest:

req.UseDefaultCredentials = true;
req.PreAuthenticate = true;
req.Credentials = CredentialCache.DefaultCredentials;
Daniel
  • 2,780
  • 23
  • 21
  • 1
    Worked like a charm, notice the use of default credentials from the cache some additional information would be nice as to why. – Niklas Aug 02 '17 at 07:09
  • Awesome. Worked for me. I kept getting 401 error but I knew I was an authorize user to my url since I was able to access the url directly (copy pasting in browser). Thanks for this. – coderguy Sep 12 '22 at 13:49
10

I think the issue is because authentication on the test IIS server. Two options:

1) Enable "Anonymous Authentication" for the Site on test IIS Server.

2) (Recommended) Before making the request to test server use the code template below with right username/password/domain information that can be authenticated against the test server.

System.Net.NetworkCredential netCredential = 
        new System.Net.NetworkCredential("<USER_NAME>", "<PASSWORD>", "<DOMAIN>");
req.Credentials = netCredential;
Chandu
  • 81,493
  • 19
  • 133
  • 134
  • 1
    Sorry to take so long to respond, I've just come back around to this bug. Will this work if we are using NT authentication? I've tried passing in my own username and password - and its still not working. – Daniel Apr 12 '11 at 00:50
  • I think it is important to point out that the code above for #2 will work great with one caveat. Make sure you do not have the property "UseDefaultCredientials" (for example UseDefaultCredientials = false;) after the line specified above. Doing this will effectively "clear out" the credientials you just set. – Bham503 Oct 02 '14 at 17:45
3

As of right now I don't have access to the IIS settings so I couldn't enable Anonymous Authentication which is very possible why Cybernate's answer was not working for me. I did find however a simpler method that worked. Instead of using a WebRequest I found that I could do the same thing with Server.Execute. Below is my new solution:

string strHTML = String.Empty;
using (var sw = new StringWriter())
{
    Server.Execute([path-to-local-aspx], sw);
    strHTML = sw.ToString();
}

string relativeReportPath = [relative-path-to-new-html-file];

using (StreamWriter writer = File.CreateText(Server.MapPath(relativeReportPath)))
{
    writer.WriteLine(strHTML);
    MessageLabel.Text = "Your report is ready. Click Close to close the window.";
}
Daniel
  • 2,762
  • 3
  • 25
  • 24
  • 1
    I just noticed I got a few negative votes for this. Obviously this is old but could someone explain why they downvoted? Thanks. – Daniel Jul 11 '13 at 22:39
  • I guess your solution works well on your situation, most of us end up here looking for a solution found @Daniel solution being correct one. Anyways, I didn't down vote yours, its just a guess why someone would have down voted yours. – Esen Oct 19 '16 at 12:39
1

Maybe a proxy bothers you. Look here to see how to disable it: How to remove proxy from WebRequest and leave DefaultWebProxy untouched

Also, maybe you have missing slash at the of your endpoint:

Endpoint = $"https://your.api.com/id=111/"; /* <-- attention to the trailing slash */
Andrii Viazovskyi
  • 777
  • 2
  • 9
  • 15