简体   繁体   English

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

[英]Credentials with WCF service in WP8 application

I have a WCF SOAP web service i would use in a Windows Phone 8 application. 我有WCF SOAP Web服务,可以在Windows Phone 8应用程序中使用。

Here is web.config on my service : 这是我的服务上的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>

I need to send credentials when i call my service because he is under a reverse proxy. 致电服务时,我需要发送凭据,因为他在反向代理下。

I cannot find an other way than this to call my service with credentials : 除此以外,我找不到其他方法来使用凭据调用我的服务:

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

With this method, i got an exception like that : 使用这种方法,我得到了一个像这样的异常:

System.ServiceModel.ProtocolException exception was not handled by user HResult code = 2146233087 Message = The remote server returned an unexpected response: (401) Authorization Required. System.ServiceModel.ProtocolException异常未由用户处理HResult代码= 2146233087消息=远程服务器返回了意外的响应:(401)需要授权。 In Silverlight, a 404 response code may be reported even when the service sends a different error code. 在Silverlight中,即使服务发送其他错误代码,也可能会报告404响应代码。 Source = System.ServiceModel StackTrace: [...] 来源= System.ServiceModel StackTrace:[...]

I'm sure that my credentials are fine, but i'm pretty sure i dont send them with the good way. 我确定我的凭据很好,但是我可以肯定我不会以很好的方式发送它们。 If it's not that, it can be my security in web.config who is false. 如果不是那样,那可能是我在web.config中的安全性是错误的。

When i try to call this service in a ConsoleApplicationProject, i got this exception : 当我尝试在ConsoleApplicationProject中调用此服务时,出现此异常:

System.ServiceModel.Security.MessageSecurityException exception was not handled HResult = 2146233087 Message = the HTTP request is unauthorized with client authentication scheme 'Anonymous'. 尚未处理System.ServiceModel.Security.MessageSecurityException异常HResult = 2146233087消息=使用客户端身份验证方案“匿名”对HTTP请求进行了未授权的操作。 The authentication header received from the server was 'Basic realm = "Foooo" '. 从服务器收到的身份验证标头是“基本领域=“ Foooo”“。 [...] [...]

Can someone plz post a sample with a webservice call in WP8 with Credentials ? 有人可以在带有凭据的WP8中通过Web服务调用发布示例吗?

If you can use <security mode="TransportCredentialOnly" /> then your problem will be resolved 如果您可以使用<security mode="TransportCredentialOnly" />那么您的问题将得到解决

Just to note endpoint address is missing in config you provided , i assume you have done so not to disclose your url. 只是要注意,您提供的配置中缺少端点地址,我想您已经这样做了,所以不公开您的网址。

Changes in client web.config 客户端web.config中的更改

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

Changes in code to call service : 更改呼叫服务代码:

    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);
}

Changes in service web.config 服务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>

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

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