2013-10-27 65 views
1

目前我在使用EWS时遇到问题,原来的要求是连接到特定用户的邮件,然后搜索特定文件夹,搜索邮件然后将附件下载到本地计算机的特定文件夹,以便稍后可以由Integration Services处理该附件,但我不想使用EWS托管API,因为我不想在迁移到生产服务器时安装任何内容,现在我的想法是开发一个WCF服务,通过.asmx uri连接到EWS服务,并且能够满足我的要求,现在我可以连接到公司的EWS,但我有一些疑问:ExchangeServiceBinding,EWS,Exchange Web服务

根据微软:http://msdn.microsoft.com/en-us/library/exchange/bb408524(v=exchg.150).aspx他们说你必须添加一个我们b服务引用到你的项目,尽管它只提到VS 2005和VS 2008(我正在使用VS 2012),它应该自动创建类ExchangeServiceBinding,它为你提供了一切你需要的模仿一个帐户,我的第一个问题是:为什么我没有看到这个课程添加到我的项目中?我应该期待这是因为我使用VS 2012吗?

好吧,无论如何,我可以使用一些方法,如getPasswordExpirationTime,但是当我使用FindFolder方法时,我收到错误:“该帐户没有模仿所请求的用户的权限”,这是我的方法:

using (ExchangeServicePortTypeClient exchangeServicePortTypeClient = new ExchangeServicePortTypeClient()) 
       { 
        exchangeServicePortTypeClient.ChannelFactory.Endpoint.Address = new EndpointAddress("https://email.kraft.com/EWS/Exchange.asmx"); 


        FindFolderResponseType findFolderResponseType = null; 

        exchangeServicePortTypeClient.FindFolder(
         new ExchangeImpersonationType 
         { 
          ConnectingSID = new ConnectingSIDType 
          { 
           Item = @"[email protected]", 
           ItemElementName = ItemChoiceType1.PrimarySmtpAddress 
          } 
         }, 
         null, 
         new RequestServerVersion { Version = ExchangeVersionType.Exchange2010_SP2 }, 
         null, 
         new FindFolderType 
         { 
          FolderShape = new FolderResponseShapeType 
          { 
           BaseShape = 
            DefaultShapeNamesType.Default 
          } 
         }, out findFolderResponseType); 

       }     

我试图通过这个设置凭据:

exchangeServicePortTypeClient.ClientCredentials.Windows.ClientCredential.UserName 
exchangeServicePortTypeClient.ClientCredentials.Windows.ClientCredential.Password  
exchangeServicePortTypeClient.ClientCredentials.Windows.ClientCredential.Domain    
exchangeServicePortTypeClient.ChannelFactory.Credentials.Windows.ClientCredential.UserName 
exchangeServicePortTypeClient.ChannelFactory.Credentials.Windows.ClientCredential.Password 
exchangeServicePortTypeClient.ChannelFactory.Credentials.Windows.ClientCredential.Domain 

没有运气:(

顺便说一下,这是我的WCF的conf ig文件。

<?xml version="1.0" encoding="utf-8"?> 

<configuration> 

    <!--Diagnostics section, we will only catch error and warning in production--> 

    <system.diagnostics> 
     <sources> 
      <source propagateActivity="true" name="System.ServiceModel" switchValue="Error, Warning"> 
       <listeners> 
        <add type="System.Diagnostics.DefaultTraceListener" name="Default"> 
         <filter type="" /> 
        </add> 
        <add type="System.Diagnostics.DefaultTraceListener" name="SellOut.ExchangeWcfService"> 
         <filter type="" /> 
        </add> 
        <add name="ServiceModelTraceListener"> 
         <filter type="" /> 
        </add> 
       </listeners> 
      </source> 
      <source name="System.ServiceModel.MessageLogging" switchValue="Error, Warning"> 
       <listeners> 
        <add type="System.Diagnostics.DefaultTraceListener" name="Default"> 
         <filter type="" /> 
        </add> 
        <add type="System.Diagnostics.DefaultTraceListener" name="SellOut.ExchangeWcfService"> 
         <filter type="" /> 
        </add> 
        <add name="ServiceModelMessageLoggingListener"> 
         <filter type="" /> 
        </add> 
       </listeners> 
      </source> 
     </sources> 
     <sharedListeners> 
      <add initializeData="C:\Users\LFH2623\Documents\SellOut\SellOut\SellOut.Hosts.ExchangeWcfService\SellOut.ExchangeWcfService_web_tracelog.svclog" 
       type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
       name="ServiceModelTraceListener" traceOutputOptions="LogicalOperationStack, DateTime, Callstack"> 
       <filter type="" /> 
      </add> 
      <add initializeData="C:\Users\LFH2623\Documents\SellOut\SellOut\SellOut.Hosts.ExchangeWcfService\SellOut.ExchangeWcfService_web_messages.svclog" 
       type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
       name="ServiceModelMessageLoggingListener" traceOutputOptions="LogicalOperationStack, DateTime, Callstack"> 
       <filter type="" /> 
      </add> 
     </sharedListeners> 
     <trace autoflush="true" /> 
    </system.diagnostics> 

    <!--Framework Section--> 

    <appSettings> 
     <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> 
    </appSettings> 

    <!--Web section--> 

    <system.web>   
     <compilation debug="true" targetFramework="4.5" /> 
     <httpRuntime targetFramework="4.5" maxRequestLength="2147483646" /> 
    </system.web> 

    <!--Web server section--> 

    <system.webServer> 
     <directoryBrowse enabled="false"/> 
     <defaultDocument enabled="true"/>    
     <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 

    <!--WS section--> 

    <system.serviceModel> 

     <diagnostics> 
      <messageLogging logMalformedMessages="true" 
          maxMessagesToLog="10000" 
          logMessagesAtTransportLevel="true" 
          logMessagesAtServiceLevel="True" 
          logEntireMessage="true"/> 
      <endToEndTracing activityTracing="false" /> 
     </diagnostics> 

     <!--For the ExchangeWebService we will aply the following service and endpoint behaviors--> 

     <behaviors> 

      <serviceBehaviors> 
       <behavior name="ExchangeWebService"> 
        <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" /> 
        <serviceDebug includeExceptionDetailInFaults="false" httpHelpPageEnabled="true" /> 
        <dataContractSerializer maxItemsInObjectGraph="2147483646" /> 
        <serviceTimeouts transactionTimeout="01:00:00" /> 
        <serviceThrottling maxConcurrentCalls="100" maxConcurrentSessions="100" maxConcurrentInstances="100"/> 
        <serviceDiscovery> 
         <announcementEndpoints> 
          <endpoint kind="udpAnnouncementEndpoint"></endpoint> 
         </announcementEndpoints> 
        </serviceDiscovery> 
       </behavior>    
      </serviceBehaviors> 

      <!-- Define the corresponding scope for the clients to find the service through resolve message --> 

      <endpointBehaviors> 
       <behavior name="ExchangeWebService"> 
        <endpointDiscovery enabled="true"> 
         <scopes> 
          <add scope="http://SellOut.ExchangeWcfService/"/> 
         </scopes> 
        </endpointDiscovery> 
       </behavior> 
      </endpointBehaviors> 

     </behaviors> 

     <!-- In case you want to scale this service --> 

     <standardEndpoints> 

      <!-- We allow the service to be discoverable through the network in an adhoc architecture through UDP --> 

      <udpDiscoveryEndpoint> 
       <standardEndpoint name="adhocDiscoveryEndpointConfiguration" 
            discoveryMode="Adhoc" 
            discoveryVersion="WSDiscovery11" 
            maxResponseDelay="00:00:10"> 
       </standardEndpoint> 
      </udpDiscoveryEndpoint> 

      <!-- We allow the service to be discoverable through the network in a managed architecture --> 

      <discoveryEndpoint> 
       <standardEndpoint name="managedDiscoveryEndpoint" discoveryMode="Managed" maxResponseDelay="00:01:00"/> 
      </discoveryEndpoint> 

      <!-- We announce the service with hello & bye --> 

      <announcementEndpoint> 
       <standardEndpoint name="udpAnnouncementEndpointConfiguration" 
            discoveryVersion="WSDiscovery11" /> 
      </announcementEndpoint> 

     </standardEndpoints> 

     <!--All the Kraft's clients are .net, so we will use the proprietary binary message encoding to reduce the message's size --> 

     <bindings> 
      <customBinding> 
       <binding name="wsHttpBindingBynaryEncoding" 
         closeTimeout="00:10:00" 
         openTimeout="00:10:00" 
         receiveTimeout="00:10:00" 
         sendTimeout="00:10:00"> 
        <binaryMessageEncoding> 
         <readerQuotas maxDepth="32" 
             maxStringContentLength="5242880" 
             maxArrayLength="2147483646" 
             maxBytesPerRead="4096" 
             maxNameTableCharCount="5242880" /> 
        </binaryMessageEncoding> 
        <httpTransport 
            transferMode="Buffered" 
            maxBufferPoolSize="2147483646" 
            maxReceivedMessageSize="2147483646"> 
        </httpTransport> 
       </binding> 
      </customBinding> 

      <basicHttpBinding> 
       <binding name="KraftEWS" messageEncoding="Text" transferMode="Buffered"> 
        <security mode="Transport"> 
         <transport clientCredentialType="Windows" proxyCredentialType="Windows"></transport>       
        </security>    
       </binding> 
      </basicHttpBinding> 

     </bindings> 

     <!-- We reference the Kraft EWS --> 

     <client> 
      <endpoint binding="basicHttpBinding" bindingConfiguration="KraftEWS" 
         contract="KraftEWS.ExchangeServicePortType"          
         name="ExchangeServiceBinding_ExchangeServicePortType">     
      </endpoint> 
     </client> 

     <!--Services section--> 

     <services> 
      <service name="SellOut.Services.Exchange.ExchangeWebService" behaviorConfiguration="ExchangeWebService"> 
       <endpoint name="rules" 
          address="rules" 
          binding="customBinding" 
          bindingConfiguration="wsHttpBindingBynaryEncoding" 
          behaviorConfiguration="ExchangeWebService" 
          contract="SellOut.Contracts.Exchange.IExchangeRulesContract"/> 
       <endpoint name="udpDiscovery" 
          kind="udpDiscoveryEndpoint" 
          endpointConfiguration="adhocDiscoveryEndpointConfiguration" /> 
       <endpoint name="mex" 
          address="mex" 
          binding="mexHttpBinding" 
          contract="IMetadataExchange" />    
      </service> 
     </services> 

     <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 

    </system.serviceModel> 

</configuration> 

另一件事更何况,在这个配置:

<basicHttpBinding> 
        <binding name="KraftEWS" messageEncoding="Text" transferMode="Buffered"> 
         <security mode="Transport"> 
          <transport clientCredentialType="Windows" proxyCredentialType="Windows"></transport>       
         </security>    
        </binding> 
       </basicHttpBinding> 

如果我删除了部分客户端凭据类型“窗口”,试图运行该服务的异常,当是:

的HTTP请求未经授权,客户端身份验证方案为“匿名”。从服务器收到的验证头是'Negotiate,NTLM'。

你们可以帮我一把吗。

感谢您的建议。

回答

0

我强烈建议您为此使用EWS托管API。当你声明你不想在生产服务器上安装任何东西时,我不明白你的意思,因为你必须在生产服务器上安装WCF服务,以及由添加Web服务参考创建的对象模型项目。但是,看起来你已经通过了这部分,所以让我们继续...

非常好,很高兴知道你可以打电话给getPassowrdExpirationTime。感谢您提供给我的信息。

您收到消息“该帐户没有模拟所请求的用户的权限”的原因是因为运行您的WCF服务的帐户没有该邮箱的模拟权限。在您的服务帐户可以访问用户的帐户之前,您需要登录setup Exchange impersonation。一旦您的服务拥有Exchange可以在AD中查找的凭据(它已经拥有),并且它有权模拟您的用户(位于您的TODO列表中),则您的代码应该可以工作,因为身份验证是基于服务帐户的身份完成的。

代码示例后的问题的所有部分都不在范围内。您不需要对web.config进行任何更改(至少我不这么认为)。由于Auth方案是Negotiate和NTLM,因此我假定您正在对内部部署的服务器进行测试。

与问候,

您链接到

+0

感谢您的回复迈克尔的物品,通过在服务器上安装任何东西我的意思是,我不希望安装EWS API的服务器,该服务器已经有.NET框架4.5安装,所以我们可以认为WCF将功能确定。另外我想告诉你,我发现问题,exchangeservicebinding类不是自动创建的,因为你必须像旧服务引用一样添加服务引用,我的意思是,单击高级属性,然后添加Web引用,所以在这个类中,我能够连接指定我想要的任何凭证,而不需要模仿 –

+0

仍然迈克尔你是正确的说你在Exchange中的模拟和是的验证计划是谈判和NTLM,非常感谢迈克。 –

相关问题