gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:53:49 25 4
gpt4 key购买 nike

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

这是我的服务的 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 exception was not handled by user HResult code = 2146233087 Message = The remote server returned an unexpected response: (401) Authorization Required. In Silverlight, a 404 response code may be reported even when the service sends a different error code. Source = System.ServiceModel StackTrace: [...]

我确定我的凭据没问题,但我很确定我没有以正确的方式发送它们。如果不是那样,那可能是我在 web.config 中的安全性是错误的。

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

System.ServiceModel.Security.MessageSecurityException exception was not handled HResult = 2146233087 Message = the HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Basic realm = "Foooo" '. [...]

有人可以在 WP8 中使用 Credentials 发布带有 Web 服务调用的示例吗?

最佳答案

如果可以使用<security mode="TransportCredentialOnly" />那么你的问题就解决了

请注意您提供的配置中缺少端点地址,我假设您已经这样做了,所以不要透露您的 url。

客户端 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>

关于c# - WP8 应用程序中具有 WCF 服务的凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24648843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com