문제

httplistener와 함께 문제에 직면 해 있습니다.

양식의 요청시

http://user:password@example.com/

만들어 졌는데, 어떻게 사용자와 비밀번호를 얻을 수 있습니까? httpwebrequest에는 자격 증명 속성이 있지만 httplistenerrequest는 가지고 있지 않으며 그 속성에서 사용자 이름을 찾지 못했습니다.

도와 주셔서 감사합니다.

도움이 되었습니까?

해결책

당신이 시도하는 것은 HTTP Basic Auth를 통한 자격 증명을 통과하는 것입니다. 사용자 이름 : 암호 구문이 httplistener에서 지원되는지 확실하지 않지만, 그렇다면 기본 인증을 먼저 허용하도록 지정해야합니다.

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

요청을 받으면 다음과 함께 사용자 이름과 비밀번호를 추출 할 수 있습니다.

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

다음은 전체 설명입니다 httplistener와 함께 사용할 수있는 모든 지원되는 인증 방법 중

다른 팁

얻으십시오 Authorization 헤더. 형식은 다음과 같습니다

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

예시:

Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==

사용자 이름과 비밀번호는 결장에 제공됩니다 (이 예에서는 Aladdin:open sesame), B64- 인코딩.

먼저 기본 인증을 활성화해야합니다.

listener.AuthenticationSchemes = AuthenticationSchemes.Basic;

그런 다음 ProcessRequest 메소드에서 사용자 이름과 비밀번호를 얻을 수 있습니다.

if (context.User.Identity.IsAuthenticated)
{
    var identity = (HttpListenerBasicIdentity)context.User.Identity;
    Console.WriteLine(identity.Name);
    Console.WriteLine(identity.Password);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top