views:

196

answers:

0

Is there any example of using a WCF REST service with basic HTTP authentication from a desktop client? I am using WCF REST Contrib. and authentication works fine when a use a javascript client from the browser, but when I try to use a C# Console app. I get a BasicUnauthorizedException {"You have unsuccessfully attempted to access a secure resource."}. even though I supplied the correct username and password.

WebHttpBinding binding = new WebHttpBinding();
binding.SendTimeout = TimeSpan.FromSeconds(25);
binding.Security.Mode = WebHttpSecurityMode.TransportCredentialOnly;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;

Uri address = new Uri("http://localhost:3525/wcfrestdemo/students.svc");

WebChannelFactory<ISudentService> factory =
             new WebChannelFactory<ISudentService>(binding, address);

factory.Credentials.UserName.UserName = "jon";
factory.Credentials.UserName.Password = "123";

ISudentService proxy = factory.CreateChannel();

var response = proxy.GetStudents(2010, 4, 2); //throws an error.

Any help will be appreciated.