HttpListener: how to get http user and password?

后端 未结 3 1270
执笔经年
执笔经年 2021-02-19 18:02

I\'m facing a problem here, with HttpListener.

When a request of the form

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

is made, how can I get the

3条回答
  •  遥遥无期
    2021-02-19 18:37

    What you're attempting to do is pass credentials via HTTP basic auth, I'm not sure if the username:password syntax is supported in HttpListener, but if it is, you'll need to specify that you accept basic auth first.

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

    Once you receive a request, you can then extract the username and password with:

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

    Here's a full explanation of all supported authenitcation methods that can be used with HttpListener.

提交回复
热议问题