【问题标题】:WCF service failed in authentication on Server 2012 but OK in Win7?WCF 服务在 Server 2012 上的身份验证失败,但在 Win7 中可以吗?
【发布时间】:2014-05-24 22:48:36
【问题描述】:

我创建了一个具有基于角色的身份验证和授权的 WCF 服务。而每个操作的实现就像

[PrincipalPermission(SecurityAction.Demand, Role = RoleConstants.Customer)]
[PrincipalPermission(SecurityAction.Demand, Role = RoleConstants.CustomerStaff)]
public void DoSomething()
{
}

membership provider 和 RoleManager 是 MySql 的,而不是 GAC 的。 SSL 证书由 IIS 7 为 https 生成。

我有一些集成测试,比如

using (var client = new MyProxy("DefaultBinding_ILicensingService"))
{
    client.ClientCredentials.UserName.UserName = "AbcShop";
    client.ClientCredentials.UserName.Password = "tttttttt";

    client.DoSomething();
}

一个实例在我的 Win7 开发机器上运行,另一个在 Server 2012 的测试服务器上运行。WCF 服务的两个实例都运行良好。

但是,1 个月后,我发现 Server 2012 上的实例不再工作,并且身份验证失败,并显示以下消息。

更新:

System.ServiceModel.Security.MessageSecurityException : An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail.
---- System.ServiceModel.FaultException : An error occurred when verifying security for the message.
Stack Trace:

Server stack trace: 
   at System.ServiceModel.Channels.SecurityChannelFactory`1.SecurityRequestChannel.ProcessReply(Message reply, SecurityProtocolCorrelationState correlationState, TimeSpan timeout)
   at System.ServiceModel.Channels.SecurityChannelFactory`1.SecurityRequestChannel.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)

更新:

我观察到以下情况,排除了一些可能的原因。

  1. 两台机器上的身份验证数据库是相同的,我可以看到 WCF 服务正在执行 SQL 查询以在客户端发出请求后检索成员信息。
  2. 我对比了两台机器上的 IIS 绑定和设置,以及 web.config,除了一些地址,基本相同。
  3. 事件查看器未显示有关 WCF 服务的警告。
  4. WCF 服务实现由这个属性修饰:公共类 ErrorHandlerBehaviorAttribute:属性、IServiceBehavior、IErrorHandler,所有未捕获的异常都将转到此属性,该属性会将错误记录到日志文件中,但是,日志文件没有记录任何这件事。

我已经比较了两个实例中加载的程序集。在 Windows 7 中,系统程序集基本上来自 Windows\Microsoft.NET\assembly\GACXXX\XXX,但在 Server 2012 中,一些程序集来自 Windows\assembly\NativeImages_v4.0.30319_64,以下是位于 NativeImages 中的一些程序集:

System   
System.Activities   
System.Core   
System.Data.DataSetExtensions   
System.Drawing   
System.Enumerics
System.ServiceModel.Activities   
System.WorkflowServices   
System.XAML   
System.ServiceModel.Web

相比之下,在 Win 7 中,只有 System 来自 NativeImages。我不确定程序集或本机图像的位置是否会改变行为。

服务和客户端是在 .NET Framework 4.5 上使用 VS 2012 开发的。

还有什么可能导致 Server 2012 上的实例失败?

【问题讨论】:

  • 你能添加一个web.config吗?当您从浏览器访问服务的 WSDL 时,它是否有效?
  • 是的,当然wsdl发布是可以的。并且请求到达了服务,我可以观察到服务正在与 auth 数据库对话,并根据客户的操作请求检索成员信息。

标签: c# wcf authentication iis windows-server-2012


【解决方案1】:

问题已解决。

根源:Server 2012 中缺少 ASP .NET MVC 程序集。

背景是,我希望 WCF 服务与将来要构建的 MVC 应用程序共享相同的身份验证数据库。所以member provider和auth manager是来自MVC的,而数据库是MySql,所以provider来自一个开源的第三方组件,与MVC耦合。

我的未捕获异常处理程序和事件查看器无法捕获有关缺少程序集的警告的原因是: 1. 开发 PC 由喜欢 GAC 的先前开发人员使用。您知道 GAC 可能会在开发 PC 中引起很多问题。来自 MVC 1、2 和 3 的大量程序集在 GAC 中。 2. 与 MVC 交互的第三个组件有一些糟糕的编程习惯,在某些地方吞下了一些关于缺少程序集的异常。

在两台机器上一一比较加载的程序集后,我发现 Server 2012 上的 WCF 服务实例没有 WebMatrix.WebData.dll。在我将程序集复制到虚拟目录的bin目录后,在我复制丢失的程序集后,我收到了一连串关于更多丢失程序集的错误消息,一个一个报告。这些程序集基本上属于 MVC。错误消息出现在客户端的 HTML 服务响应中,以及服务器端的事件查看器中。

所以我现在确保部署脚本包含相应的程序集。

显然,在服务从 auth 数据库中检索到成员信息后,服务会将信息委托给 MVC 的其他程序集进行进一步处理,并且第三个组件以某种方式吞下了有关缺少程序集的异常。并且认证的其他部分得到了错误的信号,然后给出了关于“正在验证安全性...”的误导性警告信息。

教训是: 1.不要在开发机器上进行GAC。如果你从某人那里继承了一台开发机器,最好花些时间通过 XCopy 取消对某些本应是私有程序集的 gac。 2. 一些第 3 方组件可能对防御性编程有一些肮脏的做法。就我而言,该组件吞下了有关缺少程序集的警告。 3. 在调查其他可能的原因之前比较加载的程序集更便宜。我使用 Process Explorer 列出 w3wp.exe 中加载的程序集。

【讨论】:

  • 是的,GAC 很讨厌 :)。好分析。 +1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-22
  • 1970-01-01
  • 2017-05-24
  • 1970-01-01
  • 1970-01-01
  • 2010-11-24
相关资源
最近更新 更多