访问WCF服务时出错,访问被拒绝

访问WCF服务时出错,访问被拒绝,wcf,.net-4.5,Wcf,.net 4.5,我已将WCF服务迁移到安装在IIS 7.0中的.Net framework 4.5。我可以在浏览器中浏览该服务。但是,当我在控制台应用程序中引用服务并尝试调用其中的方法时,会出现错误“访问被拒绝”。下面是我正在使用的堆栈跟踪和web.config设置 System.ServiceModel.Security.SecurityAccessDeniedException was unhandled HResult=-2146233087 Message=Access is denied.

我已将WCF服务迁移到安装在IIS 7.0中的.Net framework 4.5。我可以在浏览器中浏览该服务。但是,当我在控制台应用程序中引用服务并尝试调用其中的方法时,会出现错误“访问被拒绝”。下面是我正在使用的堆栈跟踪和web.config设置

System.ServiceModel.Security.SecurityAccessDeniedException was unhandled
  HResult=-2146233087
  Message=Access is denied.
  Source=mscorlib
  StackTrace:
    Server stack trace: 
       at System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message reply, MessageFault fault, String action, MessageVersion version, FaultConverter faultConverter)
       at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
       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)
       at ConsoleApplication1.sharedservice.IShared.ProcessRequest(ProcessRequestRequest request)
       at ConsoleApplication1.sharedservice.SharedClient.ConsoleApplication1.sharedservice.IShared.ProcessRequest(ProcessRequestRequest request) in c:\Users\vijayara\Documents\Visual Studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\Service References\sharedservice\Reference.cs:line 214
       at ConsoleApplication1.sharedservice.SharedClient.ProcessRequest(String RuleName, String RequestMessage, Int32 WaitTime, Int32 ChannelID, Int32 PassThruMode, Int32 MaxResponseSize, Int32 MaxErrorText, Int32& ActualResponseSize, String& Response, Int32& ActualErrorTextSize, String& ErrorText) in c:\Users\vijayara\Documents\Visual Studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\Service References\sharedservice\Reference.cs:line 226
       at ConsoleApplication1.Program.Main(String[] args) in c:\Users\vijayara\Documents\Visual Studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 22
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

<configuration>
  <appSettings/>
  <connectionStrings/>
    <system.web>
    <compilation debug="false" targetFramework="4.5">

          <assemblies>
            <add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
            <add assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
          </assemblies>

        </compilation>
        <authentication mode="Windows" />
    <pages controlRenderingCompatibilityVersion="4.0">
        <controls>
          <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        </controls>
      </pages>

      <httpHandlers>
        <remove verb="*" path="*.asmx"/>
        <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
      </httpHandlers>
      <httpModules>
        <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      </httpModules>


    </system.web>
    <system.webServer>
      <validation validateIntegratedModeConfiguration="false"/>
      <modules>
        <add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      </modules>
      <handlers>
        <remove name="WebServiceHandlerFactory-Integrated"/>
      <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </handlers>
    </system.webServer>


  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="false" />
    <services>
      <service behaviorConfiguration="Access_Shared_WCF.SharedBehavior" name="Access_Shared_WCF.Shared">
        <endpoint address="" binding="basicHttpBinding" contract="Access_Shared_WCF.IShared">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http/localhost/Access_Shared_WCF/Shared" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Access_Shared_WCF.SharedBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>
未处理System.ServiceModel.Security.SecurityAccessDeniedException HResult=-2146233087 消息=访问被拒绝。 Source=mscorlib 堆栈跟踪: 服务器堆栈跟踪: 在System.ServiceModel.Channels.ServiceChannel.ThrowiffaultUnderstanding处(消息回复、消息故障、字符串操作、消息版本、故障转换器故障转换器) 位于System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime操作,ProxyRpc&rpc) 在System.ServiceModel.Channels.ServiceChannel.Call(字符串操作、布尔单向、ProxyOperationRuntime操作、对象[]输入、对象[]输出、时间跨度超时) 位于System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage方法调用,ProxyOperationRuntime操作) 位于System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage消息) 在[0]处重试异常: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,IMessage retMsg) at System.Runtime.Remoting.proxy.RealProxy.PrivateInvoke(MessageData&msgData,Int32类型) 在控制台应用程序1.sharedservice.IsShared.ProcessRequest(ProcessRequest请求) 位于c:\Users\vijayara\Documents\Visual Studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\Service References\sharedservice\IsShared.ProcessRequest(ProcessRequestRequest请求)中的ConsoleApplication1.sharedservice.SharedClient.ConsoleApplication1.sharedservice.IsShared.ProcessRequest请求:第214行 在控制台应用程序1.sharedservice.SharedClient.ProcessRequest(字符串规则名、字符串请求消息、Int32 WaitTime、Int32通道ID、Int32直通模式、Int32 MaxResponseSize、Int32 MaxErrorText、Int32&ActualResponseSize、字符串与响应、Int32&ActualErrorTextSize、字符串与ErrorText)在c:\Users\vijayara\Documents\Visual Studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\Service References\sharedservice\Reference.cs中:第226行 位于c:\Users\vijayara\Documents\Visual Studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\Program.Main中的ConsoleApplication1.Program(字符串[]args):第22行 位于System.AppDomain.\u nExecuteAssembly(RuntimeAssembly程序集,字符串[]args) 位于System.AppDomain.ExecuteAssembly(字符串汇编文件、证据汇编安全性、字符串[]args) 在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()上 位于System.Threading.ThreadHelper.ThreadStart\u上下文(对象状态) 位于System.Threading.ExecutionContext.RunInternal(ExecutionContext ExecutionContext、ContextCallback回调、对象状态、布尔值preserveSyncCtx) 在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态,布尔保存SyncCTX) 在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态) 位于System.Threading.ThreadHelper.ThreadStart()处 内部异常:
我对WCF也有类似的问题。问题是我的appPool帐户没有IIS生成的用于“卷影复制”的程序集的权限。 我通过使用procmon将其过滤为IIS进程名“w3wp.exe”,并将状态设置为“拒绝访问”来跟踪它。然后再次调用服务并检查procmon输出哪些文件存在此问题

我可以访问c:\windows\temp\Microsoft.Generated.dll上的deniend,因此我已将其删除,再次调用,并生成了一个新的,所有工作正常

另一个选项是将“更强大”的用户标识设置为类似本地服务的应用池(应用池->高级设置->索引->预定义帐户->本地服务)


但是使用procmon的解决方案更有趣:-)

出现此错误的原因有很多。我遇到它的次数是当wcf方法被保护到特定的AD角色时:

  • 在web服务客户端上设置ClientCredentials。这可能很挑剔,对客户端连接执行某些操作可能会导致您丢失凭据
  • 在设置ClientCredentials之前设置InnerChannel.OperationTimeout将清除ClientCredentials(我知道,但我已经测试了很多次)

  • 我认为这可能是服务试图使用事件日志来记录信息的问题。事件日志不存在,应用程序池标识伪用户没有创建事件日志的权限。因此,使用高特权帐户的建议通过允许创建它来解决问题。可以简单地将用户切换到高帐户运行,然后切换回常规运行

    或者,如果您知道需要创建什么,您可以使用powershell直接创建事件日志(注意,通常很容易查看任何.NET应用程序或dll的源代码,以根据使用情况查找常量字符串名称)


    来源:

    删除以下配置,然后尝试@Dhawalk,这并不能解决问题。您是否尝试过使用wcf测试客户端进行调试,或使用SoapUI或等效工具?尝试向服务器添加跟踪,以查看是否获得任何信息。
     New-EventLog -source "My.Name.Space" -logname "MyApplicationName"