2

是否有任何使用 WCF REST 服务和来自桌面客户端的基本 HTTP 身份验证的示例?我正在使用 WCF REST Contrib。当从浏览器使用 javascript 客户端时,身份验证工作正常,但当我尝试使用 C# 控制台应用程序时。我收到BasicUnauthorizedException {“您尝试访问安全资源失败。”}。即使我提供了正确的用户名和密码。

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.

任何帮助将不胜感激。

4

0 回答 0