没有发现WS-Security头

4

我正在尝试使用以下演示URL的服务:

[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代码和Apace一起创建的。

据我所知,这个错误与用户名和密码(身份验证)有关,但我不知道应该在哪里输入这些凭据。


尝试进行Unicommerce集成时出现相同的错误。 你解决了这个问题吗? - Sujit.Warrier
1
您可以通过以下方式传递凭据: us.ClientCredentials.UserName.username="用户名"; us.ClientCredentials.UserName.Password="密码"; - Sujit.Warrier
2个回答

2

您可以使用来自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); 这里的doc是什么? - rahularyansharma
XmlDocument doc = new XmlDocument();XmlDocument doc = new XmlDocument(); - user887983
我还添加了如何获取字符串值 - user887983
类型或命名空间“UsernameToken”无法找到?你是否缺少任何引用? - rahularyansharma
顺便说一句,如果您不需要 nonce,也可以使用 WCF 生成令牌。 - user887983
显示剩余7条评论

1
你需要了解与网络服务通信的安全要求,然后将安全头添加到你的代码中。查看一个示例这里

我已经查看了那个例子,但是不知道如何将那段代码应用到我的代码中? - rahularyansharma
你不需要精确使用这段代码,首先检查你正在处理的服务的安全要求,然后添加适当的安全头。如果服务中有纯文本用户名令牌,你可以使用这个例子。你需要使用UsernameToken,获取你的服务代理的SoapContext,然后像示例中那样设置时间戳并将用户名令牌添加到其中。 - Amir Shenouda

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接