2014-01-07 2 views
0

양식 인증을 httpwebrequest와 함께 사용하려고하는데 성공하지 못하는 것 같습니다. 내가하는 일은 다음과 같습니다.HttpWebRequest 및 인증

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 

request.Method = WebRequestMethods.Http.Get; 

// Authentication items ------------------------------------------- 
request.UseDefaultCredentials = false; 
request.PreAuthenticate = true; 

// Attempt to set username and password: 
CredentialCache cc = new CredentialCache(); 
cc.Add(
new Uri(url), 
"Basic", 
new NetworkCredential("username", "validpassword", "domain") 
); 
request.Credentials = cc; 

request.AllowAutoRedirect = true; 
request.KeepAlive = true; 
request.Timeout = 10000; 
CookieContainer cookieContainer = new CookieContainer(); 
request.CookieContainer = cookieContainer; 

// request html 
response = (HttpWebResponse)request.GetResponse(); 

도메인의 유효한 암호와 사용자 이름을 사용하는 경우에도 항상 예외가 발생합니다.

아무도 도와 줄 수 있습니까?

참고 :

  • 예외 유형 : WebException이
  • 예외 메시지 : "원격 서버가 오류 반환 :. (401) 무단를"
  • 예외 상태 : 프로토콜
  • 오류 예외 발생 : response = (HttpWebResponse) request.GetResponse();
  • 예외 스택 추적 : 없음 ... 미안
+0

에 대한 FileMon을? – sh1rts

답변

1

나는 HttpWebRequest를위한 자격 증명을 추가합니다.

myReq.UseDefaultCredentials = true; 
    myReq.PreAuthenticate = true; 
    myReq.Credentials = CredentialCache.DefaultCredentials; 

발생하는 401 오류의 하위 상태 코드를 알고 싶습니다. 401 오류에는 다음과 같은 하위 상태 코드가 포함됩니다.

401.1: Access is denied due to invalid credentials. 
401.2: Access is denied due to server configuration favoring an alternate authentication method. 
401.3: Access is denied due to an ACL set on the requested resource. 
401.4: Authorization failed by a filter installed on the Web server. 
401.5: Authorization failed by an ISAPI/CGI application. 
401.7: Access denied by URL authorization policy on the Web server. 

프로젝트가 컴퓨터에서 제대로 작동하기 때문에 프로세스 ID 문제가 아닌지 의심됩니다. 기본적으로 IIS 6.0의 응용 프로그램 풀은 "네트워크 서비스"를 ID로 사용합니다. 이 계정은 제한되어있어 액세스 거부 문제가 발생할 수 있습니다.

문제 해결을 위해 응용 프로그램 풀의 ID를 "로컬 시스템"으로 변경하십시오. 이 방법으로 문제를 해결할 수 있으면 ID를 "네트워크 서비스"로 다시 변경하고 읽고 쓸 XML 파일에 읽기/쓰기 권한을 부여하십시오. 응용 프로그램 풀의 ID를 변경하는 방법에 대한

, 다음 단계를 참조하십시오

파일에 권한을 부여하는 방법에 대한
1. Open IIS manager (Start | Control Panel | Administrative Tools | Internet Information Services Manager). 
2. Expand the “Application Pools” node. 
3. Right click the application pool which your project is using, and then select “Properties”. 
4. Click “Identity” tab. 
5. Choose “Local System” in the Predefined dropdown list. 

가 다음 단계를 확인하십시오 :

또한
1. Open Windows Explorer. 
2. Right click the file, and then select "Properties". 
3. Click the "Security" tab. 
4. Add "Network Service" in access list and check "Modify", "Read", and "Write" for it. 

, 다른 파일도이 문제를 일으킬 수 있습니다. Filemon을 사용하여 액세스 파일을 모니터링하는 문제를 모니터링 할 수 있습니다.

내가 어떤 예외가 발생되고 우리에게 어쩌면 스택 추적하면 누군가가 당신을 도울 수있는 확신 윈도우 v7.04 http://www.microsoft.com/technet/sysinternals/FileAndDisk/Filemon.mspx

+0

WebException 유형에서 하위 상태 코드를 가져 오는 방법은 무엇입니까? –

+0

참조 http://msdn.microsoft.com/en-us/library/system.net.webexception.status(v=vs.110).aspx –