在mutualcertificate ws-security header wcf中添加用户名

时间:2014-03-05 22:00:48

标签: c# web-services wcf soap ws-security

经过1周的研究和尝试/重试,我确实成功地编写了我的需求。

我目前正面临有关ws-security签名消息的问题。

所以我使用soap12和ws-security 1.0,框架4.0以及authenticationMode中的共同证书。

除了我需要在ws-security标头中输入用户名之外,发送到webservice的请求都很好。

如果我放了CertificateOverTransport,我有用户名,但邮件签名不够。

这是我的约束力:

<customBinding>
<binding name="NewBinding">
    <textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
        messageVersion="Soap12" writeEncoding="utf-8">
    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
    </textMessageEncoding>
    <security includeTimestamp="true"
            authenticationMode="MutualCertificate"
                securityHeaderLayout="Strict"
            defaultAlgorithmSuite="Basic256"
            allowSerializedSigningTokenOnReply="true"
            messageSecurityVersion="WSSecurity10WSTrust13WSSecureConversation13WSSecurityPolicy12BasicSecurityProfile10">      
    </security>
    <httpsTransport manualAddressing="false" maxBufferPoolSize="524288"
        maxReceivedMessageSize="65536" allowCookies="false" authenticationScheme="Anonymous"
        bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
        keepAliveEnabled="true" maxBufferSize="65536" proxyAuthenticationScheme="Anonymous"
        realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false"
        useDefaultWebProxy="true" requireClientCertificate="true" />
</binding>
</customBinding>

这是我的行为:

<behaviors>
    <endpointBehaviors>
    <behavior name="ServiceBehavior">
        <clientCredentials>
        <clientCertificate findValue="XXXXX" storeName="My" storeLocation="CurrentUser" x509FindType="FindByThumbprint"/>
        <serviceCertificate>
            <authentication certificateValidationMode ="PeerOrChainTrust" />
            <defaultCertificate findValue="XXXX" storeLocation="CurrentUser" storeName="My" x509FindType="FindBySubjectName" />
        </serviceCertificate>
        </clientCredentials>         
    </behavior>
    </endpointBehaviors>
</behaviors>

我的终点:

<client>
    <endpoint address="xxxx" binding="customBinding"
        bindingConfiguration="NewBinding" contract="WSSTestOutboundService"
        name="NewPort.0"  behaviorConfiguration="ServiceBehavior">
    <identity>
        <dns value="XXXX"/>
    </identity>
    </endpoint>
</client>

这是我的代码:

System.Net.ServicePointManager.ServerCertificateValidationCallback +=
(se, cert, chain, sslerror) =>
{
    return true;
};

WSSTestOutboundServiceClient test = new WSSTestOutboundServiceClient();
test.ClientCredentials.ClientCertificate.SetCertificate(
                        StoreLocation.CurrentUser
                    , StoreName.My
                    , X509FindType.FindByThumbprint
                    , "9f 28 4b 80 f1 fe 5c 9e ea 4d b4 a1 34 48 e2 47 b9 29 82 27");

test.ClientCredentials.ServiceCertificate.SetDefaultCertificate(
                        StoreLocation.CurrentUser
                    , StoreName.My
                    , X509FindType.FindBySubjectName
                    , "DPUPRGWYDP01.npr.bngf.local");
test.Endpoint.Contract.ProtectionLevel = System.Net.Security.ProtectionLevel.Sign;

test.ClientCredentials.UserName.UserName = "TEST";
test.ClientCredentials.UserName.Password = "TEST";
test.ChannelFactory.Credentials.UserName.UserName = "TEST2";
test.ChannelFactory.Credentials.UserName.Password = "TEST2";

getGreeting test2 = new getGreeting();
MessageBox.Show(test.getGreeting(test2).greeting);

请你能帮帮我吗?

httprequest中的用户名不够好。

谢谢!

//////////// UPDATE ////////////////

我设法通过在端点信息中手动添加此用户名令牌来添加:

<endpoint address="https://XXXXXXX:443/TestSecurity/V1" binding="customBinding"
          bindingConfiguration="NewBinding" contract="WSSTestOutboundService"
          name="NewPort.0"  behaviorConfiguration="ServiceBehavior">
  <identity>
    <dns value="DPUPRGWYDP01.npr.bngf.local"/>
  </identity>
  <headers>
    <wsse:UsernameToken
      xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
      wsu:Id="UsernameToken-6"
      xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
      <wsse:Username name="UserNameToken" value="Username"></wsse:Username>
    </wsse:UsernameToken>
  </headers>
</endpoint>

但我不知道如何设置变量Username ...

你有什么想法吗?

谢谢!

//////////////// UPDATE //////////////

此解决方案不起作用,因为它是一个多用户应用程序,因此我无法修改配置文件。

我在这里发帖,因为它让你知道我在寻找什么。

请帮助我!

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。

以下是添加用户名的代码:

using (new OperationContextScope(test.InnerChannel))
{
    // Add a SOAP Header to an outgoing request
    //can't send username using mutualcertificate instead of using that way
    //can be better : see http://blogs.msdn.com/b/wsdevsol/archive/2014/02/07/adding-custom-messageheader-and-http-header-to-a-wcf-method-call.aspx
    MessageHeader aMessageHeader = MessageHeader.CreateHeader("UsernameToken", "http://tempuri.org", "TOTOTO");
    OperationContext.Current.OutgoingMessageHeaders.Add(aMessageHeader);

    getGreeting test2 = new getGreeting();
    MessageBox.Show(test.getGreeting(test2).greeting);
}