如何:指定通道安全凭据

Windows Communication Foundation (WCF) 服务标记允许 COM 应用程序调用 WCF 服务。大多数 WCF 服务都要求客户端指定用于身份验证和授权的凭据。当从 WCF 客户端调用 WCF 服务时,您可以在托管代码或应用程序配置文件中指定这些凭据当从 COM 应用程序调用 WCF 服务时,您可以使用 IChannelCredentials 接口指定凭据。本主题将介绍使用 IChannelCredentials 接口指定凭据的各种方法。

提示

IChannelCredentials 是一种基于 IDispatch 的接口,在 Visual Studio 环境中使用它将无法获取 IntelliSense 功能。

本文将使用Message Security Sample中定义的 WCF 服务。

指定客户端证书

  1. 在消息安全目录中运行 Setup.bat 文件以创建和安装所需的测试证书。

  2. 打开消息安全项目。

  3. [ServiceBehavior(Namespace=``http://Microsoft.ServiceModel.Samples``)] 添加到 ICalculator 接口定义中。

  4. bindingNamespace=``http://Microsoft.ServiceModel.Samples 添加到服务的 App.config 中的终结点标记中。

  5. 生成消息安全示例并运行 Service.exe。使用 Internet Explorer 浏览至服务的 URI (https://localhost:8000/ServiceModelSamples/Service) 以确保服务正在运行。

  6. 打开 Visual Basic 6.0 并创建一个新的 Standard .exe 文件。在窗体中添加一个按钮并双击该按钮,以将以下代码添加到 Click 处理程序中:

        monString = "service:mexAddress=https://localhost:8000/ServiceModelSamples/Service?wsdl"
        monString = monString + ", address=https://localhost:8000/ServiceModelSamples/Service"
        monString = monString + ", contract=ICalculator, contractNamespace=http://Microsoft.ServiceModel.Samples"
        monString = monString + ", binding=BasicHttpBinding_ICalculator, bindingNamespace=http://Microsoft.ServiceModel.Samples"
    
        Set monikerProxy = GetObject(monString)
    
        'Set the Service Certificate.
     monikerProxy.ChannelCredentials.SetServiceCertificateAuthentication "CurrentUser", "NoCheck", "PeerOrChainTrust"
    monikerProxy.ChannelCredentials.SetDefaultServiceCertificateFromStore "CurrentUser", "TrustedPeople", "FindBySubjectName", "localhost"
    
        'Set the Client Certificate.
        monikerProxy.ChannelCredentials.SetClientCertificateFromStoreByName "CN=client.com", "CurrentUser", "My"
        MsgBox monikerProxy.Add(3, 4)
    
  7. 运行 Visual Basic 应用程序并验证结果。

    Visual Basic 应用程序将显示一个消息框,其中包含调用 Add(3, 4) 的结果。也可使用 SetClientCertificateFromFileSetClientCertificateFromStoreByName 代替 SetClientCertificateFromStore 设置客户端证书:

    monikerProxy.ChannelCredentials.SetClientCertificateFromFile "C:\MyClientCert.pfx", "password", "DefaultKeySet"
    

提示

为使此调用生效,客户端证书在运行该客户端的计算机上应该是受信任的。

提示

如果标记格式不正确,或如果服务不可用,则对 GetObject 的调用将返回一个错误,指示“语法无效”。如果您收到此错误,请确保所使用的标记正确无误且服务可用。

指定用户名和密码

  1. 修改服务的 App.config 文件以使用 wsHttpBinding。验证用户名和密码时需要如此:

  2. clientCredentialType 设置为 UserName:

  3. 打开 Visual Basic 6.0 并创建一个新的 Standard .exe 文件。在窗体中添加一个按钮并双击该按钮,以将以下代码添加到 Click 处理程序中:

        monString = "service:mexAddress=https://localhost:8000/ServiceModelSamples/Service?wsdl"
        monString = monString + ", address=https://localhost:8000/ServiceModelSamples/Service"
        monString = monString + ", contract=ICalculator, contractNamespace=http://Microsoft.ServiceModel.Samples"
        monString = monString + ", binding=WSHttpBinding_ICalculator, bindingNamespace=http://Microsoft.ServiceModel.Samples"
    
        Set monikerProxy = GetObject(monString)
    
        monikerProxy.ChannelCredentials.SetServiceCertificateAuthentication "CurrentUser", "NoCheck", "PeerOrChainTrust"
        monikerProxy.ChannelCredentials.SetUserNameCredential "username", "password"
    
        MsgBox monikerProxy.Add(3, 4)
    
  4. 运行 Visual Basic 应用程序并验证结果。Visual Basic 应用程序将显示一个消息框,其中包含调用 Add(3, 4) 的结果。

    提示

    在本示例中,服务标记中指定的绑定已更改为 WSHttpBinding_ICalculator。另外请注意,在调用 SetUserNameCredential 时,必须提供有效的用户名和密码。

指定 Windows 凭据

  1. 在服务的 App.config 文件中将 clientCredentialType 设置为 Windows:

  2. 打开 Visual Basic 6.0 并创建一个新的 Standard .exe 文件。在窗体中添加一个按钮并双击该按钮,以将以下代码添加到 Click 处理程序中:

        monString = "service:mexAddress=https://localhost:8000/ServiceModelSamples/Service?wsdl"
        monString = monString + ", address=https://localhost:8000/ServiceModelSamples/Service"
        monString = monString + ", contract=ICalculator, contractNamespace=http://Microsoft.ServiceModel.Samples"
        monString = monString + ", binding=WSHttpBinding_ICalculator, bindingNamespace=http://Microsoft.ServiceModel.Samples"
        monString = monString + ", upnidentity=domain\userID"
    
        Set monikerProxy = GetObject(monString)
         monikerProxy.ChannelCredentials.SetWindowsCredential "domain", "userID", "password", 1, True
    
        MsgBox monikerProxy.Add(3, 4)
    
  3. 运行 Visual Basic 应用程序并验证结果。Visual Basic 应用程序将显示一个消息框,其中包含调用 Add(3, 4) 的结果。

    提示

    您必须使用有效值替换“domain”、“userID”和“password”。

指定颁发令牌

  1. 颁发令牌仅用于使用联合安全的应用程序。有关联合安全的更多信息,请参见联合令牌与颁发的令牌Federation Sample

    下面的 Visual Basic 代码示例演示如何调用 SetIssuedToken 方法:

        monString = "service:mexAddress=https://localhost:8000/ServiceModelSamples/Service?wsdl"
        monString = monString + ", address=https://localhost:8000/SomeService/Service"
        monString = monString + ", contract=ICalculator, contractNamespace=http://SomeService.Samples"
        monString = monString + ", binding=WSHttpBinding_ISomeContract, bindingNamespace=http://SomeService.Samples"
    
        Set monikerProxy = GetObject(monString)
    monikerProxy.SetIssuedToken("http://somemachine/sts", "bindingType", "binding")
    

    有关用于此方法的参数的更多信息,请参见 SetIssuedToken

另请参见

任务

如何:在联合身份验证服务上配置凭据
如何:创建联合客户端

概念

联合
WCF 中的消息安全
绑定与安全