从 asp.net 2.0 项目调用具有基本身份验证的 wcf webapi 服务

发布于 2024-12-22 06:42:03 字数 235 浏览 1 评论 0原文

我正在开发一个项目,该项目使用 wcf webapi 将其所有功能公开为 Web 服务。然后我用不同的观点来消费这些(目前有两种)。其中一种观点是遗留的 asp.net 2.0 项目,一旦这个新项目具有同等功能,该项目最终将被淘汰。我试图通过向项目添加服务引用来使用这些服务,但无法使其工作,因为 wcf api 使用基本的 http 身份验证,并且我无法在向导中对其进行配置。如何手动将服务引用添加到我的 ASP.NET 项目中?

谢谢!

I'm working on a project that uses wcf webapi to expose all it's functionality as web services. I then consume these with various views (well two for now). One of these views is a legacy asp.net 2.0 project that will eventually be phased out once this new project has feature parity. I'm trying to consume these services by adding a service reference to the project but can't get it to work because the wcf api uses basic http auth and I can't configure that in the wizard. How do I manually add the service reference to my asp.net project?

Thanks!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一袭白衣梦中忆 2024-12-29 06:42:03

使用 WCF Web API 时,您不使用服务引用,而是使用新的 HttpClient,例如:

var client = new HttpClient();
var byteArray = Encoding.ASCII.GetBytes(userName + ":" + password);
client.DefaultRequestHeaders.Authorization = 
    new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
var task = client.GetAsync("http://webapi/contact/1");
var contact = task.ContinueWith(
    t => {
        return t.Result.Content.ReadAsAsync<Contact>();
}).Unwrap().Result;

如果需要使用 .NET 2.0,则可以使用 HttpWebRequest(HttpClient 示例依赖于 .NET 4.0,因为它是 WCF 的一部分)网络 API):

Uri myUri = new Uri("http://webapi/contact/1");
WebRequest myWebRequest = HttpWebRequest.Create(myUri);

HttpWebRequest myHttpWebRequest = (HttpWebRequest)myWebRequest;

NetworkCredential myNetworkCredential = 
    new NetworkCredential(username, password);

CredentialCache myCredentialCache = new CredentialCache();
myCredentialCache.Add(myUri, "Basic", myNetworkCredential);

myHttpWebRequest.PreAuthenticate = true;
myHttpWebRequest.Credentials = myCredentialCache;

WebResponse myWebResponse = myWebRequest.GetResponse();

Stream responseStream = myWebResponse.GetResponseStream();

When working with WCF Web API, you don't use service references but the new HttpClient instead e.g.:

var client = new HttpClient();
var byteArray = Encoding.ASCII.GetBytes(userName + ":" + password);
client.DefaultRequestHeaders.Authorization = 
    new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
var task = client.GetAsync("http://webapi/contact/1");
var contact = task.ContinueWith(
    t => {
        return t.Result.Content.ReadAsAsync<Contact>();
}).Unwrap().Result;

If you need to use .NET 2.0, you can use the HttpWebRequest (the HttpClient sample relies on .NET 4.0 as it is part of WCF Web API):

Uri myUri = new Uri("http://webapi/contact/1");
WebRequest myWebRequest = HttpWebRequest.Create(myUri);

HttpWebRequest myHttpWebRequest = (HttpWebRequest)myWebRequest;

NetworkCredential myNetworkCredential = 
    new NetworkCredential(username, password);

CredentialCache myCredentialCache = new CredentialCache();
myCredentialCache.Add(myUri, "Basic", myNetworkCredential);

myHttpWebRequest.PreAuthenticate = true;
myHttpWebRequest.Credentials = myCredentialCache;

WebResponse myWebResponse = myWebRequest.GetResponse();

Stream responseStream = myWebResponse.GetResponseStream();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文