2008-12-10 73 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(); 
相關問題