Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpListener: how to get http user and password?

I'm facing a problem here, with HttpListener.

When a request of the form

http://user:[email protected]/

is made, how can I get the user and password ? HttpWebRequest has a Credentials property, but HttpListenerRequest doesn't have it, and I didn't find the username in any property of it.

Thanks for the help.

like image 700
FWH Avatar asked Jul 18 '09 11:07

FWH


People also ask

How do I provide a username and password in HTTP request?

We can do HTTP basic authentication URL with @ in password. We have to pass the credentials appended with the URL. The username and password must be added with the format − https://username:password@URL.

What is an HTTP listener?

An HTTP listener, also known as a network listener, is a listen socket that has an Internet Protocol (IP) address, a port number, a server name, and a default virtual server. Each virtual server provides connections between the server and clients through one or more listeners.

What is HttpListener in c#?

The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborative, hypermedia information systems. HTTP is the foundation of data communication for the World Wide Web. HttpListener is a simple, programmatically controlled HTTP protocol listener. It can be used to create HTTP servers.

What is HttpListener prefix?

For example, to receive all requests sent to port 8080 when the requested URI is not handled by any other HttpListener, the prefix is " http://*:8080/ ". Similarly, to specify that the HttpListener accepts all requests sent to a port, replace the host element with the " + " character, " https://+:8080/ ".


1 Answers

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.

like image 189
Matt Brindley Avatar answered Sep 22 '22 17:09

Matt Brindley