C# Windows Phone 8上的BasicHttpBinding安全性
我有一个自我托管的WCF服务,具有简单的用户名/密码验证。托管服务的conosle应用程序中的代码“安全”部分是:C# Windows Phone 8上的BasicHttpBinding安全性,c#,wcf,windows-phone-8,C#,Wcf,Windows Phone 8,我有一个自我托管的WCF服务,具有简单的用户名/密码验证。托管服务的conosle应用程序中的代码“安全”部分是: BasicHttpBinding b = new BasicHttpBinding(); b.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; //add
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 phone上如何使用user/pass creditentials,这就是我目前所拥有的:
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文件中没有任何特殊配置。好的,所以我解决了这个问题,我遇到了很多问题:
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>
<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";
private void单击(对象发送者,路由目标){
ServiceClient sc=新的ServiceClient();
使用(OperationContextScope范围=新的OperationContextScope(sc.InnerChannel)){
HttpRequestMessageProperty请求=新建HttpRequestMessageProperty();
request.Headers[System.Net.HttpRequestHeader.Authorization]=“Basic”+EncodeBasicAuthenticationCredentials(“test”、“test123”);
OperationContext.Current.OutgoingMessageProperties.Add(HttpRequestMessageProperty.Name,请求);
sc.setpushurisync(pushChannel.ChannelUri.ToString());
}
}
私有字符串编码基本身份验证凭据(字符串用户名、字符串密码){
字符串凭据=用户名+“:”+密码;
var ascicredentials=(来自凭证中的c)
选择c您可以执行以下操作:
客户端配置文件:
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>
<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";
它是托管在IIS中还是托管在IIS Express/WebDev中?如果有人知道更好的方法,无需对用户名/密码进行编码并手动设置标题,请添加您的答案。