WP8应用程序中具有WCF服务的凭据

时间:2014-07-09 08:32:15

标签: c# wcf windows-phone-8 soap

我有一个WCF SOAP Web服务,我将在Windows Phone 8应用程序中使用。

这是我服务上的web.config:

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

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="ersteinb">
          <security mode="TransportWithMessageCredential"/>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint contract="PDAErsteinService.IPDAErsteinMobileService" binding="basicHttpBinding" bindingConfiguration="ersteinb" name="PDAErsteinMobileServiceEndPoint"/>
    </client>
    <behaviors>
      <serviceBehaviors>
        <behavior> 
          <!-- Pour éviter la divulgation des informations sur les métadonnées, définissez la valeur ci-dessous sur false et supprimez le point de terminaison des métadonnées ci-dessus avant le déploiement. -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- Pour recevoir les détails d'exception des erreurs à des fins de débogage, définissez la valeur ci-dessous sur true. Définissez-la sur false avant le déploiement pour éviter la divulgation des informations d'exception. -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

当我打电话给我的服务时,我需要发送凭证,因为他是在反向代理下。

我找不到另外一种方法来通过凭证来调用我的服务:

PDAErsteinMobileServiceClient client = new PDAErsteinMobileServiceClient();
client.clientCredentials.UserName.UserName = "foo";
client.clientCredentials.UserName.Password = "foofoo";
client.GetAttelageCollectionCompleted += client_GetAttelageCollectionCompleted;
client.GetAttelageCollectionAsync();

使用这种方法,我得到了一个例外:

  

System.ServiceModel.ProtocolException异常未由用户处理HResult代码= 2146233087消息=远程服务器返回了   意外响应:(401)需要授权。在Silverlight中,a   即使服务发送a,也可以报告404响应代码   不同的错误代码。 Source = System.ServiceModel StackTrace:[...]

我确信我的凭据很好,但我很确定我不会用好的方式发送它们。如果不是这样,那么web.config中的安全性可能是假的。

当我尝试在ConsoleApplicationProject中调用此服务时,我遇到了这个异常:

  

System.ServiceModel.Security.MessageSecurityException异常是   未处理HResult = 2146233087 Message = HTTP请求   未经授权的客户端身份验证方案'匿名'。该   从服务器收到的身份验证标头是'Basic realm =   “Foooo”'。 [...]

有人可以在带有凭据的WP8中使用webservice调用发布样本吗?

1 个答案:

答案 0 :(得分:0)

如果您可以使用<security mode="TransportCredentialOnly" />,则问题将得到解决

只是要注意您提供的配置中缺少端点地址,我认为您这样做是为了不泄露您的网址。

客户端web.config中的更改

<bindings>
      <basicHttpBinding>
        <binding name="ersteinb">
          <security mode="TransportCredentialOnly"/> //note this change
        </binding>
      </basicHttpBinding>
    </bindings>

调用服务的代码更改:

    PDAErsteinMobileServiceClient client = new PDAErsteinMobileServiceClient();
    using (OperationContextScope scope = new OperationContextScope(client.InnerChannel)) {
                HttpRequestMessageProperty request = new HttpRequestMessageProperty();
                request.Headers[System.Net.HttpRequestHeader.Authorization] = "Basic " + EncodeCredentials("foo", "foofoo");
                OperationContext.Current.OutgoingMessageProperties.Add(HttpRequestMessageProperty.Name, request);
                client.GetAttelageCollectionAsync();
                }

//use this function 
private string EncodeCredentials(string username, string password) {
    string credentials = username + ":" + password;
    var asciiCredentials = (from c in credentials
                            select c <= 0x7f ? (byte)c : (byte)'?').ToArray();

    return Convert.ToBase64String(asciiCredentials);
}

服务web.config中的更改

<system.serviceModel>
    <services>
      <service behaviorConfiguration="ValidatorServiceBehaviour"
               name="WCFServiceLibrary.SettingsService">
        <endpoint binding="basicHttpBinding"
                  bindingConfiguration="ValidatorBinding"
                  contract="PDAErsteinService.IPDAErsteinMobileService"  />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/PDAErsteinService/" />
          </baseAddresses>
        </host>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="ValidatorServiceBehaviour">
          <serviceDebug httpsHelpPageEnabled="true"
                        includeExceptionDetailInFaults="true" />
          <serviceMetadata httpGetEnabled="true" />
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom"
                                    customUserNamePasswordValidatorType="UserValidator.Validator, UserValidator" />
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <bindings>
      <basicHttpBinding>
        <binding name="ValidatorBinding">
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Basic"/>
          </security>
        </binding>

      </basicHttpBinding>
    </bindings>

  </system.serviceModel>