首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在互斥证书ws-安全头wcf中添加用户名

在互斥证书ws-安全头wcf中添加用户名
EN

Stack Overflow用户
提问于 2014-03-05 22:00:47
回答 1查看 1.4K关注 0票数 0

经过一周的研究和尝试/重试,我成功地完成了我的需求规划。

我目前遇到了一个有关ws的问题--消息中的安全签名。

因此,我使用的是soap12与ws-Security1.0,Framework4.0和authenticationMode中的相互证书。

在发送到webservice的请求中,除了我需要在ws-安全头中放一个用户名之外,这一切都很好。

如果我放置CertificateOverTransport,我有用户名,但是消息没有足够的签名。

这是我的装订:

代码语言:javascript
复制
<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>

以下是我的行为:

代码语言:javascript
复制
<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>

我的端点:

代码语言:javascript
复制
<client>
    <endpoint address="xxxx" binding="customBinding"
        bindingConfiguration="NewBinding" contract="WSSTestOutboundService"
        name="NewPort.0"  behaviorConfiguration="ServiceBehavior">
    <identity>
        <dns value="XXXX"/>
    </identity>
    </endpoint>
</client>

这是我的代码:

代码语言:javascript
复制
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中的用户名不够好。

谢谢!

/更新/

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

代码语言:javascript
复制
<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>

但我不知道怎么设置变量用户名..。

你知不知道?

谢谢!

/更新/

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

我让这个帖子在这里是因为它让你知道我在寻找什么。

请帮帮我!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-14 15:00:16

我已经找到了解决办法。

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

代码语言:javascript
复制
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);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22210299

复制
相关文章

相似问题

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