views:

1046

answers:

3

When accessing my site, the user has to enter his credentials. They are basically plain directory access credentials. At a certain point I check if a certain file they want to download exists by calling

WebRequest req = HttpWebRequest.Create(checkUri.AbsoluteUri);
WebResponse res = req.GetResponse();

Although I can access the checkUri from the browser, I get a 401 when doing the above check. I think I have to set the

req.Credentials

But I don't know where the current credentials are stored...

Any ideas?

--Update--

  • Integrated Windows Authentication: No
  • Allow Anonymous: Off
  • Caler: Link on page of same site (GET)
  • Impersonation: default is off (don't even know how to enable it in asp.net mvc)
A: 

I think you want this:

req.Credentials = CredentialCache.DefaultCredentials;
mookid8000
Well, I tried both DefaultCredentials and DefaultNetworkCredentials.Both nuthin'
borisCallens
Is your site running with Integrated Windows Authentication turned on and Allow Anonymous off? What is making the call? If your caller is itself a website, do you have impersonation turned on and Kerberos delegation properly configured?
Dave Markle
Thx for the comment. Not sure about impersonation and kerberos delegation. How do I check?Put what I know in OP.
borisCallens
+1  A: 

You're going to need to enable Integrated Windows Authentication.

I don't know what happens in ASP.NET MVC, but in ASP.NET Web Forms impersonation is turned on by:

<identity impersonate="true">

in web.config.

Dave Markle
A: 

I was having a similar problem on a site where I'm using forms authentication, I was able to solve this problem by using the code provided here as the second reply in the thread.

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
// Add the current authentication cookie to the request
HttpCookie cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
Cookie authenticationCookie = new Cookie(
    FormsAuthentication.FormsCookieName,
    cookie.Value,
    cookie.Path,
    HttpContext.Current.Request.Url.Authority);

req.CookieContainer = new CookieContainer();
req.CookieContainer.Add(authenticationCookie);

WebResponse res = req.GetResponse();
Dave