[Mono-list] Basic authentication with HttpListener

Pedro Santos Gomez pedrosantosgomezlists at yahoo.es
Wed Jan 24 08:13:52 EST 2007


I'm having problems with enabling Basic authentication in System.Net.HttpListener. I am trying code from  a lot of forums and so  on, with no result.

This code, for example:

using System.IO;
using System.Web;
using System.Net;
using System;

public class A { static void Main(){

HttpListener listener = new HttpListener();

/* Here is the important code */
listener.AuthenticationSchemeSelectorDelegate += delegate{
   Console.WriteLine("Asking for authentication scheme");
   return AuthenticationSchemes.Basic;

/* end of important code */

HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
Console.WriteLine("Received: {0} {1} from {2}",request.HttpMethod,request.Url,request.RemoteEndPoint);
if(context.User == null){
     Console.WriteLine("No user!");
    Console.WriteLine(context.User.Identity == null);
    HttpListenerBasicIdentity identity = (HttpListenerBasicIdentity)context.User.Identity;
    Console.WriteLine("Name = {0}; password = {1}",identity.Name,identity.Password);
HttpListenerResponse response = context.Response;
string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;

} }

and entering in http://localhost:1234/ returns the Hello World... but it never says "Asking for authentication scheme" and context.User is null. And I don't find any way to force the browser to ask for authentication. Neither even entering in http://user:sample@localhost:1234/ helps.

Replacing the "important code" by:
listener.AuthenticationSchemes = AuthenticationSchemes.Basic;
does have the same effect.

Did anybody manage to make this work under Mono? Thank you very much.
By the way, I'm running:
$ mcs --version
Mono C# compiler version
$ mono --version
Mono JIT compiler version, (C) 2002-2006 Novell, Inc and Contributors. www.mono-project.com
        TLS:           normal
        GC:            Included Boehm (with typed GC)
        SIGSEGV:       normal
        Disabled:      none
from the Mono generic Linux on x86 installer.


LLama Gratis a cualquier PC del Mundo.
Llamadas a fijos y móviles desde 1 céntimo por minuto.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-list/attachments/20070124/50e273c6/attachment.html 

More information about the Mono-list mailing list