Windows Phone 8上的BasicHttpBinding安全性

时间:2014-01-06 13:25:31

标签: c# wcf windows-phone-8

我有一个带有简单用户名/密码验证的自托管WCF服务。承载服务的conosle应用程序中代码的“安全”部分是:

BasicHttpBinding b = new BasicHttpBinding();
b.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
//add endpoint
selfHost.AddServiceEndpoint(typeof(ISettings), b, "SettingsService");

//add creditential check
selfHost.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom;
selfHost.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new CustomValidator();

但是我无法弄清楚我的Windows手机上要做什么来使用用户/通过信用额度,这是我到目前为止所做的:

BasicHttpBinding httpBinding = new BasicHttpBinding();
httpBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;

sc = new SettingsClient(httpBinding, new EndpointAddress("http://" + addressField.Text + "/IC/SettingsService"));
sc.ClientCredentials.UserName.UserName = "test";
sc.ClientCredentials.UserName.Password = "test123";

这总是会返回401错误。此外,我的xml文件中没有任何特殊配置。

2 个答案:

答案 0 :(得分:1)

您可以这样做:

客户端配置文件:

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpsBinding_ITestService">
              <security mode="TransportWithMessageCredential" />
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="https://test/TestService.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpsBinding_ITestService"
            contract="ITestService" name="BasicHttpsBinding_ITestService" />
    </client>
</system.serviceModel>

客户代码:

 var client = new TestServiceClient("BasicHttpsBinding_ITestService");
 client.ClientCredentials.UserName.UserName = "User name";
 client.ClientCredentials.UserName.Password = "password";

答案 1 :(得分:0)

好的,所以我想出来了,我遇到了很多问题:

  1. 我从另一个项目运行自托管服务,但我在与我的服务相同的项目中有App.config文件。不知道它必须在你运行服务的同一个项目中,愚蠢的我。

  2. 我现在拥有xml文件中的所有配置,但我可能会把它放在代码中,它仍然有用。

  3. 请参阅此LINK,它介绍了如何在WP7上进行身份验证(适用于WP8)。

  4. 确保在更改服务内容时更新服务参考。

  5. 客户代码:

    private void Click(object sender, RoutedEventArgs e) {
            ServiceClient sc = new ServiceClient();
            using (OperationContextScope scope = new OperationContextScope(sc.InnerChannel)) {
                HttpRequestMessageProperty request = new HttpRequestMessageProperty();
                request.Headers[System.Net.HttpRequestHeader.Authorization] = "Basic " + EncodeBasicAuthenticationCredentials("test", "test123");
                OperationContext.Current.OutgoingMessageProperties.Add(HttpRequestMessageProperty.Name, request);
                sc.setPushUriAsync(pushChannel.ChannelUri.ToString());
                }
    
    }
    
    private string EncodeBasicAuthenticationCredentials(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);
    }
    

    客户端配置文件:

    <configuration>
        <system.serviceModel>
            <bindings>
                <basicHttpBinding>
                    <binding name="BasicHttpBinding_ISettings" maxBufferSize="2147483647"
                        maxReceivedMessageSize="2147483647">
                        <security mode="TransportCredentialOnly" />
                    </binding>
                </basicHttpBinding>
            </bindings>
            <client>
                <endpoint address="http://MACHINE_IP_ADDR:8045/MyService/" binding="basicHttpBinding"
                    bindingConfiguration="BasicHttpBinding_ISettings" contract="SettingsServiceReference.ISettings"
                    name="BasicHttpBinding_ISettings" />
            </client>
        </system.serviceModel>
    </configuration>
    

    服务器配置文件:

    <configuration>
      <system.serviceModel>
        <services>
          <service behaviorConfiguration="ValidatorServiceBehaviour"
                   name="WCFServiceLibrary.SettingsService">
            <endpoint binding="basicHttpBinding"
                      bindingConfiguration="ValidatorBinding"
                      contract="WCFServiceLibrary.ISettings"  />
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8045/MyService/" />
              </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>
    </configuration>
    

    验证器只是一个标准UserNamePasswordValidator