WcfRestContrib:是否有从桌面客户端使用 WCF REST 服务(具有基本身份验证)的示例

发布于 2024-08-27 15:34:14 字数 909 浏览 11 评论 0原文

是否有从桌面客户端使用 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.

任何帮助将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文