我如何可以通过在头一个用户名/密码到SOAP WCF服务 [英] How can I pass a username/password in the header to a SOAP WCF Service

查看:206
本文介绍了我如何可以通过在头一个用户名/密码到SOAP WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图消耗的第三方网络服务
<一href=\"https://staging.identitymanagement.lexisnexis.com/identity-proofing/services/identityProofingServiceWS/v2?wsdl\">https://staging.identitymanagement.lexisnexis.com/identity-proofing/services/identityProofingServiceWS/v2?wsdl

我已经增加一条,作为一个服务引用,但我不知道怎么传的凭据头。

我怎样才能让头请求匹配这种格式?

&LT; soapenv:页眉和GT;
    &LT; WSSE:安全soapenv:mustUnderstand属性=1的xmlns:WSSE =htt​​p://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd&GT ;
        &LT; WSSE:用户名令牌WSU:ID =用户名令牌-49的xmlns:WSU =htt​​p://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd &GT;
            &LT; WSSE:用户名&gt; 12345 /用户ID&LT; / WSSE:用户名&gt;
            &LT; WSSE:密码类型=htt​​p://docs.oasis-open.org/wss/2004/01/绿洲-200401-WSS-用户名令牌瞩目-1.0#PasswordText&GT; password123&LT; / WSSE:密码&GT ;
            &LT; WSSE:杜撰EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary\">d+VxCZX1cH/ieMkKEr/ofA==</wsse:Nonce>
            &LT; WSU:创建&GT; 2012-08-04T20:25:04.038Z&LT; / WSU:创建&GT;
        &LT; / WSSE:用户名令牌&GT;
    &LT; / WSSE:安全和GT;
&LT; / soapenv:页眉和GT;


解决方案

有可能是一个更聪明的办法,但你可以手动添加标题是这样的:

  VAR的客户=新IdentityProofingService.IdentityProofingWSClient();使用(新OperationContextScope(client.InnerChannel))
{
    OperationContext.Current.OutgoingMessageHeaders.Add(
        新SecurityHeader(用户名令牌-49,12345 /用户ID,password123));
    client.invokeIdentityService(新IdentityProofingRequest());
}

在这里, SecurityHeader 是一个自定义类实现的,这需要我既然选择了使用属性来配置XML序列化的几个其他类:

 公共类SecurityHeader:的MessageHeader
{
    私人只读的UsernameToken _usernameToken;    公共SecurityHeader(字符串ID,用户名字符串,字符串密码)
    {
        _usernameToken =新的UsernameToken(ID,用户名,密码);
    }    公众覆盖字符串名称
    {
        {返回安全; }
    }    公众覆盖字符串命名空间
    {
        {返回http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd }
    }    保护覆盖无效OnWriteHeaderContents(作家的XmlDictionaryWriter,MessageVersion messageVersion)
    {
        XmlSerializer的序列化=新的XmlSerializer(typeof运算(用户名令牌));
        serializer.Serialize(作家,_usernameToken);
    }
}
[XmlRoot(命名空间=htt​​p://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd)]
公共类的UsernameToken
{
    公共用户名令牌()
    {
    }    公共用户名令牌(字符串ID,用户名字符串,字符串密码)
    {
        ID = ID;
        用户名=用户名;
        密码=新密码(){值=密码};
    }    [XmlAttribute(命名空间=htt​​p://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd)]
    公共字符串ID {搞定;组; }    [的XmlElement]
    公共字符串用户名{获得;组; }    [的XmlElement]
    公共密码密码{搞定;组; }
}公共类密码
{
    公共密码()
    {
        TYPE =htt​​p://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText;
    }    [XmlAttribute]
    公共字符串类型{搞定;组; }    [XMLTEXT]
    公共字符串值{获得;组; }
}

我还没有加入现时标志位到的UsernameToken XML,但它是非常相似的密码之一。在创建元素也需要还增加,但它是一个简单的 [的XmlElement]

I'm trying to consume a third-party web service https://staging.identitymanagement.lexisnexis.com/identity-proofing/services/identityProofingServiceWS/v2?wsdl

I already added it as a service reference but I'm not sure how to pass the credentials for the header.

How can I make the header request match this format?

<soapenv:Header>
    <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
        <wsse:UsernameToken wsu:Id="UsernameToken-49" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <wsse:Username>12345/userID</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/ oasis-200401-wss-username-token-profile-1.0#PasswordText">password123</wsse:Password>
            <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">d+VxCZX1cH/ieMkKEr/ofA==</wsse:Nonce>
            <wsu:Created>2012-08-04T20:25:04.038Z</wsu:Created>
        </wsse:UsernameToken>
    </wsse:Security>
</soapenv:Header>

解决方案

There is probably a smarter way, but you can add the headers manually like this:

var client = new IdentityProofingService.IdentityProofingWSClient();

using (new OperationContextScope(client.InnerChannel))
{
    OperationContext.Current.OutgoingMessageHeaders.Add(
        new SecurityHeader("UsernameToken-49", "12345/userID", "password123"));
    client.invokeIdentityService(new IdentityProofingRequest());
}

Here, SecurityHeader is a custom implemented class, which needs a few other classes since I chose to use attributes to configure the XML serialization:

public class SecurityHeader : MessageHeader
{
    private readonly UsernameToken _usernameToken;

    public SecurityHeader(string id, string username, string password)
    {
        _usernameToken = new UsernameToken(id, username, password);
    }

    public override string Name
    {
        get { return "Security"; }
    }

    public override string Namespace
    {
        get { return "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"; }
    }

    protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(UsernameToken));
        serializer.Serialize(writer, _usernameToken);
    }
}


[XmlRoot(Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
public class UsernameToken
{
    public UsernameToken()
    {
    }

    public UsernameToken(string id, string username, string password)
    {
        Id = id;
        Username = username;
        Password = new Password() {Value = password};
    }

    [XmlAttribute(Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")]
    public string Id { get; set; }

    [XmlElement]
    public string Username { get; set; }

    [XmlElement]
    public Password Password { get; set; }
}

public class Password
{
    public Password()
    {
        Type = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText";
    }

    [XmlAttribute]
    public string Type { get; set; }

    [XmlText]
    public string Value { get; set; }
}

I have not added the Nonce bit to the UsernameToken XML, but it is very similar to the Password one. The Created element also needs to be added still, but it's a simple [XmlElement].

这篇关于我如何可以通过在头一个用户名/密码到SOAP WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆