C# WP8应用程序中WCF服务的凭据

C# WP8应用程序中WCF服务的凭据,c#,wcf,windows-phone-8,soap,C#,Wcf,Windows Phone 8,Soap,我有一个WCFSOAPWeb服务,可以在WindowsPhone8应用程序中使用 以下是我的服务上的web.config: <?xml version="1.0" encoding="utf-8"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <system.s

我有一个WCFSOAPWeb服务,可以在WindowsPhone8应用程序中使用

以下是我的服务上的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>
使用此方法,我得到了如下异常:

用户未处理System.ServiceModel.ProtocolException异常HResult code=2146233087消息=远程服务器返回了 意外响应:(401)需要授权。在银色的灯光下,一个 404即使在服务发送消息时,也可能报告响应代码 不同的错误代码。Source=System.ServiceModel StackTrace:[…]

我确信我的证书是好的,但是我很确定我没有用好的方式发送它们。如果不是这样的话,可能是web.config中的安全设置为false

当我尝试在ConsoleApplicationProject中调用此服务时,出现以下异常:

System.ServiceModel.Security.MessageSecurityException异常为 未处理HResult=2146233087消息=HTTP请求未处理 客户端身份验证方案“匿名”未经授权。这个 从服务器接收的身份验证标头为“基本域”= “fooo”。[……]

有人可以用凭证在WP8中发布一个带有webservice调用的示例吗?

如果您可以使用
,那么您的问题将得到解决

请注意,您提供的配置中缺少端点地址,我假设您这样做是为了不公开您的url

客户端web.config中的更改

<bindings>
      <basicHttpBinding>
        <binding name="ersteinb">
          <security mode="TransportCredentialOnly"/> //note this change
        </binding>
      </basicHttpBinding>
    </bindings>
<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>

//注意这个变化
呼叫服务代码的更改:

    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);
}
pdaersteinmobileseserviceclient=新的pdaersteinmobileseserviceclient();
使用(OperationContextScope范围=新的OperationContextScope(client.InnerChannel)){
HttpRequestMessageProperty请求=新建HttpRequestMessageProperty();
request.Headers[System.Net.HttpRequestHeader.Authorization]=“Basic”+EncodeCredentials(“foo”,“foooo”);
OperationContext.Current.OutgoingMessageProperties.Add(HttpRequestMessageProperty.Name,请求);
GetAttelageCollectionAsync();
}
//使用此功能
私有字符串编码凭据(字符串用户名、字符串密码){
字符串凭据=用户名+“:”+密码;
var ascicredentials=(来自凭证中的c)

选择c Hello,我已经完成了您所说的所有操作,但我仍然会收到以下错误:“System.ArgumentException:提供的URI方案“https”无效;应为“http”。参数名称:via“,如果我将配置更改为“transport”,我已经粘贴了其他错误。它与RP的配置一起工作(他使用cookies,我不发送请求),HTTPS的安全模式是:TransportWithMessageCredential
<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>