【问题标题】:Windows Phone Error Handling: WCF with BasicHttpBinding and TransportWithMessageCredentialWindows Phone 错误处理:带有 BasicHttpBinding 和 TransportWithMessageCredential 的 WCF
【发布时间】:2014-05-14 08:45:04
【问题描述】:

我有一个 WCF 服务,在提供正确的凭据时可以按预期工作。

当我尝试使用 错误凭据 使用服务时,服务按预期发送 MessageSecurityException 错误,我收到错误:"MessageSecurityException was unhandled by user code"

我不确定如何处理此异常,因为它是在自动生成的 Reference.cs 文件中引发的,并且不受我的控制:

References.cs

public string EndLogin(System.IAsyncResult result) {
    object[] _args = new object[0];
    string _result = ((string)(base.EndInvoke("Login", _args, result))); //Here is the error raised
    return _result;
}

理想的做法是检查service 是否接受了凭据,而不是依赖引发的错误,但不知道如何检查。

希望有人可以帮助我,这样我的应用就不必在每次错误登录时崩溃;)

Web.config:服务:

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>

    <services>
      <service name="BiBasicService.SalesMarketingService">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicHttpBinding"
          contract="BiBasicService.ISalesMarketingService" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>

    <bindings>
      <basicHttpBinding>
        <binding name="basicHttpBinding">
          <security mode="TransportWithMessageCredential">
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>

    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpsGetEnabled="true" 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"/>

          <!-- To enable custom Role validation -->
          <serviceAuthorization principalPermissionMode="Custom">
            <authorizationPolicies>
              <add policyType="BiBasicService.Security.AuthorizationPolicy, BiBasicService" />
            </authorizationPolicies>
          </serviceAuthorization>

          <!-- To enable custom Username and Password validator-->
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="BiBasicService.Security.CustomValidator, BiBasicService"/>
          </serviceCredentials>

        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="false" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

ServiceReferences.ClientConfig:客户端:

<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_ISalesMarketingService" maxBufferSize="2147483647"
                    maxReceivedMessageSize="2147483647">
                    <security mode="TransportWithMessageCredential" />
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="https://PUBLICDOMAIN/BasicHttp/SalesMarketingService.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISalesMarketingService"
                contract="ServiceReference1.ISalesMarketingService" name="BasicHttpBinding_ISalesMarketingService" />
        </client>
    </system.serviceModel>
</configuration>

【问题讨论】:

    标签: c# .net windows web-services wcf


    【解决方案1】:

    MessageSecurityException:这是一个绑定错误。

    确保server sideclient side 上的binding configuration 必须匹配。 请张贴serverweb.configclientweb.config

    【讨论】:

      【解决方案2】:

      您可能想查看IErrorHandler 接口,它允许您在更“全局级别”处理异常。 IErrorHandler 是一个扩展,允许在抛出异常时显式控制应用程序的行为,实现 IErrorHandler 接口并将其添加到 Dispatcher 的 ErrorHandlers 属性。 IErrorHandler 使您能够显式控制生成的 SOAP 错误,决定是否将其发送回客户端,并执行相关任务,例如日志记录。错误处理程序按照它们添加到 ErrorHandlers 属性的顺序被调用。

      http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.ierrorhandler.aspx
      http://blogs.msdn.com/b/carlosfigueira/archive/2011/06/07/wcf-extensibility-ierrorhandler.aspx

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-25
        • 2011-06-03
        • 1970-01-01
        • 1970-01-01
        • 2011-12-02
        • 1970-01-01
        相关资源
        最近更新 更多