Witam,
Mam samohostujący webserwis WCF. Konfiguracja serwera wygląda mniej więcej tak:
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="NewBehavior">
<serviceMetadata httpsGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<wsHttpBinding>
<binding name="Binding">
<security mode="Transport">
<transport clientCredentialType="Basic"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="NewBehavior" name="HelloService">
<endpoint address="https://localhost:8000"
binding="wsHttpBinding"
bindingConfiguration="Binding"
name="TestWCFService.Http"
contract="HelloService"
>
</endpoint>
</service>
</services>
</system.serviceModel>
</configuration>
Certyfikat wygenerowany, podpisany i skonfigurowany na port 8000.
Kod serwera:
ServiceHost HService = new ServiceHost(typeof(HelloService));
HService.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom;
HService.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new ServerUserNamePasswordValidor();
HService.Open();
Console.WriteLine("Uruchomiono.");
Console.ReadKey();
HService.Close();
No i prosty validator hasła:
public sealed class ServerUserNamePasswordValidor : UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
if (null == userName || null == password)
{
throw new ArgumentNullException();
}
if (!(userName == "test1" && password == "1tset") && !(userName == "test2" && password == "2tset"))
{
throw new FaultException("Unknown Username or Incorrect Password");
}
}
}
Klient:
client.servRefTest.HelloServiceClient proxy = new client.servRefTest.HelloServiceClient();
ServicePointManager.ServerCertificateValidationCallback = delegate {return true;};
proxy.ClientCredentials.UserName.UserName = "test1";
proxy.ClientCredentials.UserName.Password = "1tset";
try
{
Console.WriteLine(proxy.Hello());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Co się dzieje? Ano wszystko działa dobrze, gdy serwer i klient są na tej samej maszynie. Jeżeli jednak próbuję połączyć sie z webserwisem z innego komputera w sieci okazuje się to niemożliwe - klient sypie takim wyjątkiem:
The HTTP request was forbidden with client authentication scheme 'Basic'.
Nie dochodzi więc nawet do walidacji użytkownika na serwerze - żądanie jest od razu odrzucane. Dlaczego tak się dzieje?
Dodam jeszcze, że bez autoryzacji (czyli gdy clientCredentialType="None") działa wszystko dobrze. Zależy mi dokładnie na takiej autoryzacji (username/password - żadne username token i inne nie wchodzą w grę).
Co robie zle?