简体   繁体   English

Windows Phone错误处理:具有BasicHttpBinding和TransportWithMessageCredential的WCF

[英]Windows Phone Error Handling: WCF with BasicHttpBinding and TransportWithMessageCredential

I have a WCF service that works as expected when providing proper credentials. 提供适当的凭据时,我有一个WCF服务可以正常工作。

When I try to consume the service with wrong credentials , the service sends an MessageSecurityException error as expected, and I receive an error: "MessageSecurityException was unhandled by user code" . 当我尝试使用错误的凭证使用该服务时,该服务将按预期方式发送MessageSecurityException错误,并且收到错误消息: "MessageSecurityException was unhandled by user code"

I'm not sure how to handle this exception, since it is raised in the Reference.cs file that is auto-generated and not really under my control: 我不确定如何处理此异常,因为该异常是在自动生成的Reference.cs文件中引发的,并且不在我的控制之下:

References.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;
}

Ideal would be to check if the service has accepted the credentials instead of relying on an error raised, but have no idea how to check this. 理想的方法是检查service是否已接受凭据,而不是依靠引发的错误,但不知道如何检查此错误。

Hope someone can help me, so my App don't have to crash on each wrong login ;) 希望有人可以帮助我,这样我的应用程序就不必在每次错误登录时都崩溃;)

Web.config : 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 : Client: 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>

The MessageSecurityException: it's a binding error. MessageSecurityException:这是一个绑定错误。

Make sure the binding configuration on server side and client side must match. 确保server sideclient sidebinding configuration必须匹配。 Please post the server side web.config and client side web.config 请发布serverweb.configclientweb.config

You may want to look into the IErrorHandler interface, which would allow you to handle the exception at a more “global level”. 您可能需要研究IErrorHandler接口,该接口使您可以在更“全局”的级别上处理异常。 The IErrorHandler is an extension that allows explicitly control the behavior of the application when an exception is thrown, implement the IErrorHandler interface and add it to the Dispatcher's ErrorHandlers property. IErrorHandler是一个扩展,它允许在引发异常时明确控制应用程序的行为,实现IErrorHandler接口并将其添加到Dispatcher的ErrorHandlers属性。 IErrorHandler enables you to explicitly control the SOAP fault generated, decide whether to send it back to the client, and perform associated tasks, such as logging. IErrorHandler使您可以显式控制生成的SOAP错误,确定是否将其发送回客户端,并执行相关的任务,例如日志记录。 Error handlers are called in the order in which they were added to the ErrorHandlers property. 错误处理程序按照将其添加到ErrorHandlers属性的顺序进行调用。

http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.ierrorhandler.aspx 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 http://blogs.msdn.com/b/carlosfigueira/archive/2011/06/07/wcf-extensibility-ierrorhandler.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM