2012-05-08 48 views
1

Android的安全WCF服務的Mono狀態是什麼?是否符合SOAP 1.2呢? 我正在爲Android應用程序編寫一個POC,它將與WCF服務交互,但我正努力工作。MessageSecurityException:安全處理器無法在消息中找到安全標頭

我試圖連接到具有TransportWithMessageCredential安全性的服務。但是,我在服務器端收到錯誤。

這是服務器端錯誤:

MessageSecurityException: Security processor was unable to find a security header in the message. This might be because the message is an unsecured fault or because there is a binding mismatch between the communicating parties. This can occur if the service is configured for security and the client is not using security.

服務器配置:

<service name="BrandDirector.ApplicationServer.Core.Services.UI.Products.Specifications.IngredientService" 
     behaviorConfiguration="CredentialValidation"> 
    <endpoint address="/BasicHttp" 
     binding="basicHttpBinding" 
     bindingConfiguration="BDBasicHttpBindingWithSecurity" 
     contract="BrandDirector.ApplicationServer.Core.Services.UI.Products.Specifications.IIngredientService" /> 
    <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" /> 
</service> 

<behavior name="CredentialValidation"> 
    <serviceMetadata httpGetEnabled="true" /> 
    <serviceDebug includeExceptionDetailInFaults="true" /> 
    <HttpStatusCode200Behavior /> 
    <serviceCredentials type="BrandDirector.ApplicationServer.Core.Security.Authentication.PasswordServiceCredentials, BrandDirector.ApplicationServer.Core.Security, Version=1.0.0.0, Culture=neutral"> 
    <userNameAuthentication userNamePasswordValidationMode="Custom" 
     customUserNamePasswordValidatorType="BrandDirector.ApplicationServer.Core.Security.CredentialValidator, BrandDirector.ApplicationServer.Core.Security" /> 
    </serviceCredentials> 
</behavior> 

<extensions> 
    <behaviorExtensions> 
    <add name="HttpStatusCode200Behavior" type="BrandDirector.ApplicationServer.Core.Services.Common.ServiceModel.HttpStatusCode200BehaviorExtension, BrandDirector.ApplicationServer.Core.Services.Common" /> 
    </behaviorExtensions> 

    <basicHttpBinding> 
    <binding name="BDBasicHttpBindingWithSecurity" messageEncoding="Text" maxReceivedMessageSize="655536"> 
     <security mode="TransportWithMessageCredential" > 
     <message clientCredentialType="UserName" /> 
     </security> 
    </binding> 
    </basicHttpBinding> 
</extensions> 

客戶端代碼:

public class Activity1 : Activity 
{ 
    private Button button; 
    const string address = "https://.../IngredientService.svc/BasicHttp"; 

    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 


     var timeout = new TimeSpan(0, 1, 0); 
     var binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential) 
     { 
      MessageEncoding = WSMessageEncoding.Text, 
      Security = 
      { 
       Transport = 
       { 
        ClientCredentialType = HttpClientCredentialType.None, 
        ProxyCredentialType = HttpProxyCredentialType.None 
       }, 
       Message = 
       { 
        ClientCredentialType = BasicHttpMessageCredentialType.UserName, 
       } 
      }, 
      HostNameComparisonMode = HostNameComparisonMode.StrongWildcard, 
      MaxReceivedMessageSize = 655536, 
      ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas 
      { 
       MaxArrayLength = 655536, 
       MaxStringContentLength = 655536, 
      }, 
      SendTimeout = timeout, 
      OpenTimeout = timeout, 
      ReceiveTimeout = timeout, 
     }; 

     System.Net.ServicePointManager.ServerCertificateValidationCallback += OnServerCertificateValidationCallback; 

     // Set our view from the "main" layout resource 
     SetContentView(Resource.Layout.Main); 

     // Get our button from the layout resource, 
     // and attach an event to it 
     button = FindViewById<Button>(Resource.Id.MyButton); 

     button.Click += delegate 
     { 
      client = new IngredientServiceClient(binding, new EndpointAddress(address)); 
      var clientCredential = client.ClientCredentials.UserName; 
      clientCredential.UserName = "admin"; 
      clientCredential.Password = "KDNSG7"; 

      client.BeginGetIngredients("e", callBack, null); 
     }; 
    } 

    IngredientServiceClient client; 

    private void callBack(IAsyncResult ar) 
    { 
     var result = client.EndGetIngredients(ar); 

     button.Text = result.First().Name; 
    } 

    private bool OnServerCertificateValidationCallback(object sender, X509Certificate certificate, 
                 X509Chain chain, SslPolicyErrors sslPolicyErrors) 
    { 
     return true; 
    } 
} 

此代碼工作正常在WPF,返回結果和一切都很好。我從老主題看到,WCF在開發週期中還處於早期階段,但我只是想檢查一下,也許我是先做錯了什麼。

回答

0

我只是使用RestSharp和一個自定義的身份驗證頭內容,這些內容將在服務器端進行身份驗證。

2

爲了使您的服務儘管結合不匹配工作,你需要修改

<security mode="TransportWithMessageCredential" enableUnsecuredResponse="true" > 
    <message clientCredentialType="UserName" /> 
</security> 

我已經包括從例子中,整個webconfig上面添加的變化

<service name="BrandDirector.ApplicationServer.Core.Services.UI.Products.Specifications.IngredientService" 
     behaviorConfiguration="CredentialValidation"> 
    <endpoint address="/BasicHttp" 
     binding="basicHttpBinding" 
     bindingConfiguration="BDBasicHttpBindingWithSecurity" 
     contract="BrandDirector.ApplicationServer.Core.Services.UI.Products.Specifications.IIngredientService" /> 
    <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" /> 
</service> 

<behavior name="CredentialValidation"> 
    <serviceMetadata httpGetEnabled="true" /> 
    <serviceDebug includeExceptionDetailInFaults="true" /> 
    <HttpStatusCode200Behavior /> 
    <serviceCredentials type="BrandDirector.ApplicationServer.Core.Security.Authentication.PasswordServiceCredentials, BrandDirector.ApplicationServer.Core.Security, Version=1.0.0.0, Culture=neutral"> 
    <userNameAuthentication userNamePasswordValidationMode="Custom" 
     customUserNamePasswordValidatorType="BrandDirector.ApplicationServer.Core.Security.CredentialValidator, BrandDirector.ApplicationServer.Core.Security" /> 
    </serviceCredentials> 
</behavior> 

<extensions> 
    <behaviorExtensions> 
    <add name="HttpStatusCode200Behavior" type="BrandDirector.ApplicationServer.Core.Services.Common.ServiceModel.HttpStatusCode200BehaviorExtension, BrandDirector.ApplicationServer.Core.Services.Common" /> 
    </behaviorExtensions> 

    <basicHttpBinding> 
    <binding name="BDBasicHttpBindingWithSecurity" messageEncoding="Text" maxReceivedMessageSize="655536"> 
     <security mode="TransportWithMessageCredential" enableUnsecuredResponse="true" > 
     <message clientCredentialType="UserName" /> 
     </security> 
    </binding> 
    </basicHttpBinding> 
</extensions> 
+1

請注意,enableUnsecuredResponse不適用於basicHttpBinding。它適用於customBinding,但。 – thomasb

0

在我的情況下,在固定的問題,與同一異常主機配置文件audienceUris標籤添加服務。

<system.identityModel> 
... 
    <identityConfiguration> 
    ... 
     <audienceUris> 
      <add value="serviceName.svc" /> 
     </audienceUris> 
    ... 
    </identityConfiguration> 
... 
</system.identityModel>