2008-12-10 75 views
3

访问我的站点时,用户必须输入他的凭据。他们基本上是普通的目录访问凭证。 在某一点上我检查,如果他们要下载某个文件通过调用获取WebRequest中使用的当前请求的凭据

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

虽然我可以从浏览器访问checkUri存在,我得到一个401做上述检查时。我想我必须设置

req.Credentials 

但我不知道目前的凭据存储在何处?

任何想法?

--Update--

  • 集成Windows身份验证:没有
  • 允许匿名:关
  • Caler:链接同一网站(GET)
  • 假冒的页面上:默认关闭(甚至不知道如何启用它在asp.net mvc)

回答

5

我想你想要这样的:

req.Credentials = CredentialCache.DefaultCredentials; 
+1

嗯,我试了DefaultCredentials和DefaultNetworkCredentials。 nuthin' – 2008-12-10 09:48:34

1

你将需要启用集成Windows身份验证。

我不知道在ASP.NET MVC中发生了什么,但在ASP.NET Web窗体模拟被打开:

<identity impersonate="true"> 

在web.config中。

6

我在使用表单身份验证的网站上遇到了类似的问题,我可以通过使用提供的代码here作为线程中的第二个回复来解决此问题。

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(); 
相关问题