【问题标题】:WCF as a Service Reference in UWP with SecurityWCF 作为具有安全性的 UWP 中的服务参考
【发布时间】:2016-07-28 09:12:44
【问题描述】:

我们将 WCF 项目添加为服务引用。当我们配置没有安全性的通信客户端时,一切正常。

没有安全保障

<security mode="None" />

现在 UWP 应用上的 .cs 文件没有安全配置。

var binding = new NetTcpBinding(SecurityMode.None);
binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.None;
client = new ServiceReference1Client(binding, new EndpointAddress(new Uri("net.tcp://localhost:1234/ServiceReference1")));

这个在 .exe.config 中没有安全性的句子就像在 Windows 8.1 中一样工作

为 .exe.config 添加安全性

<security mode="Transport">
    <transport clientCredentialType="Windows"/>
</security>

现在是具有相同安全配置的 UWP 应用上的 .cs 文件。

var binding = new NetTcpBinding(SecurityMode.Transport);
binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
client = new ServiceReference1Client(binding, new EndpointAddress(new Uri("net.tcp://localhost:1234/ServiceReference1")));

使用安全配置运行应用程序会引发此异常:

  • 例外: System.PlatformNotSupportedException
  • 留言: "SecureConnectionDuplexSession is not supported."

这里是 StrackTrace:

System.ServiceModel.Channels.FramingDuplexSessionChannel.FramingConnectionDuplexSession.CreateSession(FramingDuplexSessionChannel channel, StreamUpgradeProvider upgrade)
System.ServiceModel.Channels.FramingDuplexSessionChannel..ctor(ChannelManagerBase factory, IConnectionOrientedTransportFactorySettings settings, EndpointAddress remoteAddresss, Uri via, Boolean exposeConnectionProperty)
System.ServiceModel.Channels.ClientFramingDuplexSessionChannel..ctor(ChannelManagerBase factory, IConnectionOrientedTransportChannelFactorySettings settings, EndpointAddress remoteAddresss, Uri via, IConnectionInitiator connectionInitiator, ConnectionPool connectionPool, Boolean exposeConnectionProperty, Boolean flowIdentity)
System.ServiceModel.Channels.ConnectionOrientedTransportChannelFactory`1.OnCreateChannel(EndpointAddress address, Uri via)
System.ServiceModel.Channels.ChannelFactoryBase`1.InternalCreateChannel(EndpointAddress address, Uri via)
System.ServiceModel.Channels.ChannelFactoryBase`1.CreateChannel(EndpointAddress address, Uri via)
System.ServiceModel.Channels.ServiceChannelFactory.ServiceChannelFactoryOverDuplexSession.CreateInnerChannelBinder(EndpointAddress to, Uri via)
System.ServiceModel.Channels.ServiceChannelFactory.CreateServiceChannel(EndpointAddress address, Uri via)
System.ServiceModel.Channels.ServiceChannelFactory.CreateChannel[TChannel](EndpointAddress address, Uri via)
System.ServiceModel.ChannelFactory`1.CreateChannel(EndpointAddress address, Uri via)
System.ServiceModel.ChannelFactory`1.CreateChannel()
System.ServiceModel.ClientBase`1.CreateChannel()
System.ServiceModel.ClientBase`1.CreateChannelInternal()
System.ServiceModel.ClientBase`1.get_Channel()
Fooooooo(String jsonRequest)
Fooooooo.<Boooooo>d__0`1.MoveNext()
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
Fooooo.<Booooo>d__5.MoveNext()
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
Foooo.<Boooooo>d__12.MoveNext()

问题

  • 我们可以做些什么来支持 WCF 即服务参考和 UWP 应用之间的安全连接。
  • 目前是否支持此功能?

编辑 1

我在 .exe.config 上看到了安全性

<security mode="Transport">
    <transport clientCredentialType="Windows"/>
</security>

客户端状态为 Opened,但 client.InnerChannel 抛出了 System.PlatformNotSupportedException

类型的异常

正如@glen-thomas 所说,我试试这个配置:

<security mode="Transport">
    <transport clientCredentialType="None"/>
</security>

但在这种情况下,客户端状态处于 Faultet 并引发了此异常

  • 例外: System.ServiceModel.CommunicationObjectFaultedException
  • 留言: "The communication object, System.ServiceModel.ChannelFactory1[Fooo], cannot be used for communication because it is in the Faulted state."

编辑 2

在github的dotnet\wcf页面上,我们可以看到目前不支持这个。

https://github.com/dotnet/wcf/blob/master/release-notes/SupportedFeatures-v1.0.0-rc1.md

【问题讨论】:

    标签: c# wcf security win-universal-app windows-10


    【解决方案1】:

    目前uwp不支持NetTcpBinding的传输安全模式。你可以使用basicHttpBinding代替NetTcpBinding。 UWP 可以使用带有 Windows 身份验证的 basicHttpBinding 的 WCF。

    【讨论】:

    • 嗨@sunteen,但是使用basicHttpBinding 我可以在不连接的情况下连接到我的服务吗?我的意思是,我不知道本地主机上的 basicHttpBinding 允许我在没有连接的情况下发送请求。对吗?
    • @dachibox,您是否认为使用basicHttpBinding 您的客户端应用程序无需连接到您的wcf 服务并且可以使用wcf?那是不对的。当然需要连接。如果您仍有新问题,我建议您将答案标记为对您有帮助并为新事物打开新线程。
    • 在 Windows 8.1 中,我们正在使用 NetTcpBinding 添加服务引用,并且在安全传输模式下工作。该服务在每台机器上本地运行,无需连接即可工作。我现在看到目前不支持 uwp 中的这个。 github.com/dotnet/wcf/blob/master/release-notes/…感谢所有@sunteen。
    【解决方案2】:

    如果连接的安全性很重要,但连接的人不重要,请尝试启用传输安全性,但将凭据类型设置为无。这将保护连接,但允许匿名客户端连接。

    <security mode="Transport">
        <transport clientCredentialType="None"/>
    </security>
    

    或者,您可以通过将企业身份验证私有网络(客户端和服务器)功能添加到您的 UWP 应用程序来解决您的问题。

    【讨论】:

    • 嗨@glen-thomas,我尝试了这个配置并在第一个await client.OpenAsync(); 调用中遇到了异常。 glen-thomas 带有此**消息:** "The communication object, System.ServiceModel.ChannelFactory1[Fooo_ServiceReference1], cannot be used for communication because it is in the Faulted state."
    • 我添加了企业身份验证和专用网络(客户端和服务器),但我得到了同样的错误。
    猜你喜欢
    • 1970-01-01
    • 2012-04-29
    • 2010-12-06
    • 1970-01-01
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多