2009-07-18 3 views
9

여기 HttpListener와 관련된 문제가 있습니다.HttpListener : http 사용자 및 암호를 얻는 방법?

형태

http://user:[email protected]/ 

의 요청이

는 어떻게 사용자와 비밀번호를 발급받을 수 있습니까? HttpWebRequest에는 Credentials 속성이 있지만 HttpListenerRequest에는 해당 속성이 없으므로 해당 속성의 사용자 이름을 찾지 못했습니다.

도움 주셔서 감사합니다.

답변

21

HTTP 기본 인증을 통해 자격 증명을 전달하는 것입니다. 사용자 이름 : 암호 구문이 HttpListener에서 지원되는지 확실하지 않지만, 그렇다면 기본 설정을 수락하도록 지정해야합니다. 먼저 인증하십시오. HTTP Listener에 함께 사용할 수있는 모든 지원 authenitcation 방법

HttpListenerBasicIdentity identity = (HttpListenerBasicIdentity)context.User.Identity; 
Console.WriteLine(identity.Name); 
Console.WriteLine(identity.Password); 

Here's a full explanation : 당신이 요청을 받으면

HttpListener listener = new HttpListener(); 
listener.Prefixes.Add(uriPrefix); 
listener.AuthenticationSchemes = AuthenticationSchemes.Basic; 
listener.Start(); 

, 당신은 다음에 사용자 이름과 암호를 추출 할 수 있습니다.

+0

죄송합니다 "나는 이름 경우 아니에요 : 암호 구문은 HttpListener에서 지원됩니다. "물론 클라이언트를"WWW-Authenticate : basic "헤더로 변환하는 클라이언트이기 때문에 클라이언트가 지원하는 경우에만 중요합니다. IE에 대한 지원이 최근에 중단 된 것 같습니다. –

4

Authorization 헤더를 가져 오십시오. 다음 B64 부호화

Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== 

사용자 이름과 비밀번호 (Aladdin:open sesame이 예에서) 콜론 구분이다 그것은

Authorization: <Type> <Base64-encoded-Username/Password-Pair> 

예 다음 형식이있다.

2

먼저 기본 인증을 사용하도록 설정해야합니다 :

listener.AuthenticationSchemes = AuthenticationSchemes.Basic; 

그런 다음 processRequest 메소드에 당신이 사용자 이름과 암호를 얻을 수 : 내가 말한

if (context.User.Identity.IsAuthenticated) 
{ 
    var identity = (HttpListenerBasicIdentity)context.User.Identity; 
    Console.WriteLine(identity.Name); 
    Console.WriteLine(identity.Password); 
} 
관련 문제