C# 在mutualcertificate ws-security标头wcf中添加用户名

C# 在mutualcertificate ws-security标头wcf中添加用户名,c#,web-services,wcf,soap,ws-security,C#,Web Services,Wcf,Soap,Ws Security,经过一周的研究和反复尝试,我成功地编写了几乎符合我需要的程序 我目前遇到一个有关ws-security登录消息的问题 因此,我将soap12与ws-security 1.0、framework 4.0以及authenticationMode中的相互证书一起使用 发送到webservice的请求中的所有内容都很好,只是我需要在ws-security头中添加一个用户名 如果我放置CertificateOverTransport,我有用户名,但消息签名不够 这是我的装订: <customBind

经过一周的研究和反复尝试,我成功地编写了几乎符合我需要的程序

我目前遇到一个有关ws-security登录消息的问题

因此,我将soap12与ws-security 1.0、framework 4.0以及authenticationMode中的相互证书一起使用

发送到webservice的请求中的所有内容都很好,只是我需要在ws-security头中添加一个用户名

如果我放置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>
你能帮我吗

httprequest中的用户名不够好

谢谢大家!

////////////更新////////////////

通过在端点信息中手动添加此用户名令牌,我成功地添加了此用户名令牌:

<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

你知道吗

谢谢大家!

////////////////更新//////////////

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

我把这篇文章放在这里是因为它让你知道我在寻找什么


请帮帮我

我找到了解决办法

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

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);
}
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);
<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>
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);
}