WCF 和多种安全模型

标签 wcf wcf-security

我们正在开发 WCF 服务来存放我们的核心 API。

我们正在开发 2 个客户端来使用此 API,其中一个是 WPF 桌面应用程序,它很可能会根据 Active Directory 进行身份验证并与 API 驻留在同一域中。另一个是 ASP.Net Web 应用程序,它很可能使用 ASP.Net Membership 来确保安全,并且仍然驻留在与 WCF 服务相同的域中。 WCF 服务计划使用 NetTcp 并托管在 Windows 服务中。

在可能的情况下,我希望 WCF 服务作为调用用户运行,我想这对于用户是域用户的桌面应用程序来说应该是相当简单的。对于 Web 应用程序,我想我需要创建一个用户来运行服务调用。

是否有可能让这种双重安全方法在单个 WCF 服务上运行,或者我是否需要制作 2 个服务,每个服务都有自己的安全模型?

此外,如果有人对实现此目标的最佳实践/模式有任何想法,那将是很棒的。

谢谢

最佳答案

我已经通过以下方式解决了同样的问题:

  1. 我为每次使用创建了两个 net.tcp 绑定(bind)。

      <security mode="TransportWithMessageCredential">
        <transport clientCredentialType="" />
        <message clientCredentialType="UserName" />
      </security>
    
    </binding>
    <binding name="WindowsBinding" >
    
      <security mode="TransportWithMessageCredential">
        <transport clientCredentialType="Windows" />
      </security>
    
    </binding>
    

  2. 接下来,我为每个服务添加了两个端点

    <service name="SimplePluginService" behaviorConfiguration="CommonBehavior">
    
       <endpoint binding="netTcpBinding" bindingConfiguration="UserNameBinding" name="SimplePluginServiceUserName" contract="ISimplePluginService">
           <identity>
                <dns value="WCfServer" />
           </identity>
       </endpoint>
    
       <endpoint binding="netTcpBinding" bindingConfiguration="WindowsBinding" name="SimplePluginServiceWindows" contract="ISimplePluginService">
           <identity>
                <dns value="WCfServer" />
           </identity>
        </endpoint>
    
    </service>
    
  3. 接下来,我在创建 ChannelFactory(ConnectionManager - 类,其中包含有关用户信用的信息)时选择了适当的端点。

    private readonly Dictionary<Type, Object> channelFactoryDictionary = new Dictionary<Type, Object>();
    
    private ChannelFactory<T> GetChannelFactory<T>() where T : class
    {
        if (channelFactoryDictionary.Keys.Contains(typeof(T)))
        {
            return channelFactoryDictionary[typeof(T)] as ChannelFactory<T>;
        }
        else
        {
            string endpointName=typeof(T).ToString();
            if (ConnectionManager.IsWindowsAuth) endpointName+="Windows";
            else  endpointName+="UserName";
    
            ChannelFactory<T> channelFactory = new ChannelFactory<T>(endpointName);
    
            if (!ConnectionManager.IsWindowsAuth){
                 channelFactory.Credentials.UserName.UserName = ConnectionManager.Password;
                 channelFactory.Credentials.UserName.Password = ConnectionManager.Password;
            }
    
            channelFactoryDictionary.Add(typeof(T), channelFactory);
            return channelFactory;
        }
    }
    

关于WCF 和多种安全模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11518843/

相关文章:

wcf - 在一个 Windows 服务中的单个 TCP 端口上托管多个 WCF 服务

c# - 具有.Net 客户端和Android 客户端的WCF 服务器策略?

由于参数大小导致 WCF 服务通信异常

c# - 仅使用 SSL 进行身份验证

c# - WCF:将用户名和密码传递给另一项服务(无 Https)

c# - WCF:缓存 SSL 数据

.net - 从 WSDL 生成服务契约

wcf - WF4/WCF 中的 RESTful 工作流服务端点

c# - WCF "A call to SSPI failed, see inner exception"

使用客户端证书的 WCF 服务需要在 IIS 中进行匿名访问,因此实际上不起作用?