【问题标题】:How to authenticate a web service in C#如何在 C# 中对 Web 服务进行身份验证
【发布时间】:2014-12-15 12:33:17
【问题描述】:

在 C#.net 项目中将 url 添加到服务引用后,我能够访问服务的不同方法,服务提供商给了我用于身份验证的用户名和密码,但是在访问属性时我没有得到字段/properties 用于用户名和密码(标题)。所以我的问题是如何验证这样的网络服务?

----------------------------------------代码------ ------------------------------------------------

queryNniNameType obj = new queryNniNameType();

reqtype = new queryNameAvailabilityRequestType();
headerType = new CNR.ServiceReference1.businessDocumentHeaderType();
businessbody = new queryNniNameType(); 

headerType.messageType = "test";
headerType.messageVersion = 2;
headerType.senderType ="sendertype";
headerType.senderId = "381133334535055664"; 
businessbody.proposedName = "asdfsf";  

reqtype.businessDocumentHeader = headerType;
reqtype.businessDocumentBody = businessbody;


nameAvailabilityResponseType nptype = new nameAvailabilityResponseType();
businessDocumentHeaderType bustype = new businessDocumentHeaderType();
req = new ExternalQueryNameAvailabilityClient();

reply = new queryNameAvailabilityReplyType();
reply.businessDocumentBody = nptype;
reply.businessDocumentHeader = bustype;

那么在哪里传递用户名和密码呢??

【问题讨论】:

  • 请显示一些无需身份验证的请求代码。
  • 你在使用 asmx 网络服务吗?
  • 是的,我正在使用基于 Soap 的 Web 服务,我只是在访问 3rd 方服务
  • @Codor 我已经给出了代码请参考
  • 或许下面的答案刚刚好;这是 WCF 吗?好像是这样。

标签: c# .net web-services httprequest


【解决方案1】:

在 WCF 服务中会是这样的:

client.ClientCredentials.UserName.UserName = "user";
client.ClientCredentials.UserName.Password = "pass";

对于 Web 服务,请改用客户端的 Credentials 属性。

【讨论】:

    【解决方案2】:

    为 web 服务和 web api 提供安全性几乎是一回事。 参考这个answer 几天前我问过并得到了接近。

    【讨论】:

      猜你喜欢
      • 2012-07-28
      • 1970-01-01
      • 2011-08-14
      • 1970-01-01
      • 2023-04-04
      • 2011-04-22
      • 1970-01-01
      • 2013-03-19
      • 2016-09-14
      相关资源
      最近更新 更多