【问题标题】:No WS-Security header found未找到 WS-Security 标头
【发布时间】:2013-06-18 05:43:33
【问题描述】:

我正在尝试使用演示网址如下的服务

[https://demo.unicommerce.com/services/soap/uniware13.wsdl?facility=01][1]

当我添加此服务并尝试在我的代码中使用它时,如下所示

using abc;
public partial class unicommerce : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        unicommerce u = new unicommerce();

        UnicommerceClient us = new UnicommerceClient();


        Customer c=new Customer ();
        PartyAddress pa=new PartyAddress ();
        pa.StateCode="25";
        pa.Pincode="302017";
        c.BillingAddress=pa;
        PartyContact p=new PartyContact ();

        c.Contact=p;
        c.CSTNumber="123";
        c.CustomerCode="ABC";
        c.Name="example";
        c.PAN="CYKPS7842";
        c.Website="http://mywebsite.in";

        CreateCustomerRequest cr = new CreateCustomerRequest();
        cr.Customer = c;
        us.CreateCustomer(cr);

    }
}

它的抛出错误

No WS-Security header found

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ServiceModel.FaultException: No WS-Security header found
  [1]: https://demo.unicommerce.com/services/soap/uniware13.wsdl?facility=01

我问过这个服务的人,他告诉我这个服务是用java代码快速创建的。

据我所知,此错误与用户名和密码(身份验证)有关,但没有得到我应该在哪里传递这些凭据。

【问题讨论】:

  • 在尝试进行 unicommerce 集成时遇到同样的错误。你发现问题了吗????
  • 您可以将凭据传递给 us.ClientCredentials.UserName.username="username"; us.ClientCredentials.UserName.Password="password";

标签: c# asp.net wcf


【解决方案1】:

你可以使用来自 Microsoft.Web.Services2 的标准 .Net WSS 实现

using Microsoft.Web.Services2.Security.Tokens;
using Microsoft.Web.Services2.Security.Utility;

UsernameToken token = new UsernameToken(username, password, passwordOption.SendHashed);          

Microsoft.Web.Services2.Security.Utility.Timestamp ts = new Timestamp();

XmlDocument doc = new XmlDocument();

XmlElement token = token.GetXml(doc);
XmlElement timestamp = ts.GetXml(doc);

string stoken = token.InnerXml;
string stimestamp = ts.InnerXml;

等等,完美运行。

Microsoft.Web.Services2.dll 可以在这里找到:

http://www.microsoft.com/downloads/details.aspx?FamilyId=FC5F06C5-821F-41D3-A4FE-6C7B56423841&displaylang=en

【讨论】:

  • XmlElement token = token.GetXml(doc);这里的文档是什么?
  • XmlDocument doc = new XmlDocument();
  • 我还添加了如何获取字符串值
  • 类型或命名空间 UsernameToken 找不到?您是否缺少任何参考资料?
  • 您需要将 Microsoft.Web.Services2 引用到您的项目中,令牌在 Microsoft.Web.Services2.Security.Tokens 中
【解决方案2】:

您必须了解与 Web 服务通信的安全要求,然后将安全标头添加到您的代码中 查看示例here

【讨论】:

  • 我已经检查了该示例,但不明白如何在我的代码中使用该代码?
  • 您不需要完全使用此代码,首先检查您正在处理的服务的安全要求是什么,然后添加适当的安全标头。如果你在服务中有普通的用户名令牌,你可以使用这个例子。您需要使用 UsernameToken,获取服务代理的 SoapContext,然后设置时间戳并将用户名令牌添加到其中,如示例中所示
猜你喜欢
  • 1970-01-01
  • 2011-02-01
  • 1970-01-01
  • 2011-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多