首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将UserNameToken传递给ASMX服务?

如何将UserNameToken传递给ASMX服务?
EN

Stack Overflow用户
提问于 2012-10-17 20:40:42
回答 1查看 1.1K关注 0票数 1

我有一个asmx web服务和一个测试控制台应用程序。我将web服务引用添加到控制台应用程序中,并按如下方式调用它

代码语言:javascript
复制
Employee.Employee e = new TestService.Employee.Employee();
e.SomeMethod();

在每个web服务调用中都有一个验证检查,如下所示

代码语言:javascript
复制
    private bool IsUserNameTokenPresent()
    {
        //Get current SOAP context
        SoapContext ctxt = RequestSoapContext.Current;
        UsernameToken user = null;
        if (ctxt == null)
        {
            //This request is using a different protocol other than SOAP.
            return false;
        }

        //Iterate through all Security tokens
        foreach(SecurityToken tok in ctxt.Security.Tokens)
        {
            if (tok is UsernameToken)
            {
                user = (UsernameToken)tok;

            }
        }
        if (user == null)
            return false;

        return true;
    }

问题:如何传递安全令牌以便测试此服务。一直都是空的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-23 22:09:49

终于找到了答案。我必须手动创建自己的SOAP头,并将其与请求一起传递。这是一些代码。我必须为每个调用动态创建Nonce,如果有人想要这样做的话,我会在这里发布它。

代码语言:javascript
复制
            XmlDocument doc = new XmlDocument();

            doc.InnerXml = @"<?xml version='1.0' encoding='utf-8'?>
                                <soap:Envelope 
                                xmlns:wsu='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd' 
                                xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' 
                                xmlns:wsse='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'>
                                <soap:Header>
                                <wsse:Security soap:mustUnderstand='1'>
                                <wsse:UsernameToken wsu:Id='uuid_faf0159a-6b13-4139-a6da-cb7b4100c10c'>
                                <wsse:Username>UserID</wsse:Username>
                                <wsse:Password>Pass</wsse:Password>
                                <wsse:Nonce>" + nonce + @"</wsse:Nonce>
                                <wsu:Created>" + date + @"</wsu:Created>
                                </wsse:UsernameToken>
                                </wsse:Security>
                                </soap:Header>
                                <soap:Body>
                                <FindBySelfId>
                                <specification>
                                <LastName>" + lastname + @"</LastName>
                                <FirstName>" + firstname + @"</FirstName>
                                <DateOfBirth>" + dob + @"</DateOfBirth>
                                <HomeZipCode>" + zip + @"</HomeZipCode>
                                <SSN4>" + ssn + @"</SSN4>
                                </specification>
                                </FindBySelfId  >
                                </soap:Body>
                                </soap:Envelope>";

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost/Employee/employee.asmx");

            req.Headers.Add("SOAPAction", "https://<Namespace here>");

            req.ContentType = "text/xml;charset=\"utf-8\"";
            req.Accept = "text/xml";

            req.Method = "POST";
            Stream stm = req.GetRequestStream();
            doc.Save(stm);
            stm.Close();
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12943015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档