WCF混合身份验证用户名和Windows

5

在 WCF 中,我们可以使用两种类型的身份验证:Windows 身份验证和用户名身份验证。通过使用消息安全模式和证书进行身份验证。我的用户名身份验证 cfg/code 如下:
服务器配置:

<?xml version="1.0"?>
  <configuration>
<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior name="ServiceCredentialsBehavior">
                <serviceCredentials>
                    <serviceCertificate findValue="cn=cool" storeName="TrustedPeople" storeLocation="CurrentUser" />
                    <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="Util.CustomUserNameValidator, Util"  />
                </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>
 </configuration>

客户端配置:

<?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>
</configuration>

如何更改服务器以知道访问它的Windows身份?

1个回答

1
有趣的问题!如果您确实需要混合验证,可以尝试将传输设置为一种验证类型,将消息设置为另一种验证类型。我不知道这在实践中是否有效,但考虑到您可以单独配置它们,这似乎是合理的:)
您可以查看是否可以设置类似于以下内容的绑定,以捕获Windows凭据(wsHttpBinding可以处理Windows凭据)。
 <security mode="Transport">
        <transport clientCredentialType="Whatever your authentication method is" />
        <message clientCredentialType="Windows" />
      </security>

如果你尝试了,请告诉我它是否有效!

编辑:

哦,根据文档,可以进行混合身份验证。您必须将模式设置为“Mixed”,因此配置可能如下所示:

 <security mode="mixed">
        <transport clientCredentialType="Whatever your authentication method is" />
        <message clientCredentialType="Windows" />
      </security>

来自文档:

混合安全。 混合安全为您提供了最佳的两个世界:传输安全确保消息的完整性和机密性,而用户凭据和声明则像消息安全一样封装在每个消息中。这使您能够使用各种用户凭据,这是严格传输安全机制所不可能的,并利用传输安全的性能。


不,它不起作用。 也许我没有正确设置Windows凭据,如何从服务器检索它们? - croisharp
你真的需要两种不同的身份验证方法吗?你可以将客户端的Windows凭据传递给服务,并允许服务模拟用户。这可能比尝试混合和匹配身份验证类型更好。如果您想要验证用户名,可以将它们存储在数据库中,并将其与从WindowsIdentity.GetCurrent()返回的对象中模拟的客户端用户详细信息进行比较(最好存储sid,因为用户名可能会更改)。 - Franchesca
是的,我需要两种身份验证方法+使用NetSqlAzMan进行授权,但我知道如何做,我的问题在于混合身份验证。 - croisharp
它会报错: 解析器错误消息:无法解析属性“mode”的值。错误是:枚举值必须是以下之一:None、Transport、Message、TransportWithMessageCredential。 - croisharp
您的绑定不支持混合模式,或者您的客户端配置与服务器配置不匹配。 - Franchesca
@Franchesca 模拟用户 - Juval Lowy 的《Programming WCF Services 3rd Edition》建议不要使用模拟身份,因为它是90年代的产物,无法扩展 - user585968

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接