17

服务器配置:

<?xml version="1.0"?>
<configuration>
<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior name="ServiceCredentialsBehavior">
                <serviceCredentials>
                    <serviceCertificate findValue="cn=cool" storeName="TrustedPeople" storeLocation="CurrentUser" />
                </serviceCredentials>
                <serviceMetadata httpGetEnabled="true" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <services>
        <service behaviorConfiguration="ServiceCredentialsBehavior" name="Service">
            <endpoint address="" binding="wsHttpBinding" bindingConfiguration="MessageAndUserName" name="SecuredByTransportEndpoint" contract="IService"/>
        </service>
    </services>
    <bindings>
        <wsHttpBinding>
            <binding name="MessageAndUserName">
                <security mode="Message">
                    <message clientCredentialType="UserName"/>
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>
    <client/>
</system.serviceModel>
<system.web>
    <compilation debug="true"/>
</system.web>

我的客户端配置:

<?xml version="1.0" encoding="utf-8"?>
  <configuration>
<system.serviceModel>
    <behaviors>
        <endpointBehaviors>
            <behavior name="LocalCertValidation">
                <clientCredentials>
                    <serviceCertificate>
                        <authentication certificateValidationMode="PeerTrust" trustedStoreLocation="CurrentUser" />
                    </serviceCertificate>
                </clientCredentials>
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <bindings>
        <wsHttpBinding>
            <binding name="WSHttpBinding_IService" >
                <security mode="Message">
                    <message clientCredentialType="UserName" />
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:48097/WCFServer/Service.svc"
                  binding="wsHttpBinding"
                  bindingConfiguration="WSHttpBinding_IService"
                  contract="ServiceReference1.IService"
                  name="WSHttpBinding_IService" behaviorConfiguration="LocalCertValidation">
            <identity>
                <dns value ="cool" />
            </identity>
        </endpoint>
    </client>
</system.serviceModel>

服务:

public string TestAccess()
{
    return OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name;
}

客户:

        ServiceClient client = new ServiceClient();
        client.ClientCredentials.UserName.UserName = "Admin";
        client.ClientCredentials.UserName.Password = "123";
        Console.WriteLine(client.TestAccess());
        Console.ReadLine();

错误:
从另一方接收到不安全或不正确安全的故障。有关故障代码和详细信息,请参阅内部 FaultException。
内部异常:
无法验证消息中的至少一个安全令牌。

如何解决此异常?

4

2 回答 2

24

我认为问题在于您的用户名和密码。使用默认配置,用户名和密码被验证为 windows 帐户。如果您想要其他验证,您必须使用会员提供者自定义用户名密码验证器

于 2011-07-01T08:36:48.947 回答
3

由于错误消息相当模糊,我想我会把它作为另一种可能的解决方案放在那里。

我的环境使用单点登录(或 STS,如果您愿意)通过 ASP.NET MVC 站点对用户进行身份验证。MVC 站点反过来通过传递它从 STS 服务器请求的承载令牌与之前的引导令牌来对我的服务端点进行服务调用。我得到的错误是当我从 MVC 站点进行服务调用时。

就我而言,这是由我的服务配置引起的,如下所述。特别是 AudienceUris 节点,它必须与服务端点完全匹配:

<system.identityModel>
    <identityConfiguration>
        <audienceUris>
            <add value="https://localhost/IdpExample.YService/YService.svc" />
        </audienceUris>
        ....
    </identityConfiguration>
</system.identityModel>

HTH。

于 2014-06-13T00:48:01.060 回答