WCF Service mit Userauthentifizierung
Folgende Herangehensweise soll zeigen, wie man einen WCF Service mit Userauthentifizierung implementiert.
Zertifikat:
Um ein Zertifikat zu erstellen, kann man das Tool Pluralsight Self-Cert Tool (Mirror) dazu verwenden. In diesem Tool (als Administrator ausführen) definiert man den Namen des Zertifikates sowie den Speicherort.
Service:
Im Service erstellen wir eine Validator-Klasse die von UserNamePasswordValidator erbt. Dort überschreibt man die Methode Validate, in der man den Usernamen und die Passwortübergabe durch den Client überprüft.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class MyCustomUserNameValidator : UserNamePasswordValidator { public override void Validate(string userName, string password) { if (null == userName || null == password) { throw new ArgumentNullException(); } if (!(userName == "test1" && password == "test1")) { throw new SecurityTokenException("Unknown Username or Password"); } } } |
app.config:
In der app.config müssen nun die Parameter konfiguriert werden.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | <?xml version="1.0" encoding="utf-8" ?> <configuration> <system.web> <compilation debug="true" /> </system.web> <system.serviceModel> <bindings> <wsHttpBinding> <binding name="Binding1"> <security mode="Message"> <message clientCredentialType="UserName" /> </security> </binding> </wsHttpBinding> </bindings> <services> <service name="SSOClientAuthentifizierungsService.Service1" behaviorConfiguration="Service1Behavior"> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress = "http://localhost:8732/Design_Time_Addresses/SSOClientAuthentifizierungsService/Service1/" /> </baseAddresses> </host> <endpoint address ="" binding="wsHttpBinding" bindingConfiguration="Binding1" contract="SSOClientAuthentifizierungsService.IService1" /> </service> </services> <behaviors> <serviceBehaviors> <behavior name="Service1Behavior"> <serviceMetadata httpGetEnabled="True"/> <serviceDebug includeExceptionDetailInFaults="False" /> <serviceCredentials> <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="SSOClientAuthentifizierungsService.MyCustomUserNameValidator, SSOClientAuthentifizierungsService"/> <clientCertificate> <authentication certificateValidationMode="PeerOrChainTrust" /> </clientCertificate> <serviceCertificate findValue="ssoTool" storeName="TrustedPeople" storeLocation="LocalMachine" x509FindType="FindBySubjectName"/> </serviceCredentials> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration> |
Client:
Der Client referenziert nun den Service und ruft über die ClientCredentials die ServiceMethoden auf:
1 2 3 4 5 6 7 8 9 10 11 | static void Main(string[] args) { Service1Client c = new Service1Client(); c.ClientCredentials.UserName.UserName = "test1"; c.ClientCredentials.UserName.Password = "test1"; var value = c.GetData(10); Console.WriteLine(value.ToString()); Console.ReadKey(); } |
Quellen:
- http://codekicker.de…
- http://msdn.microsoft.com…
- http://windows.microsoft.com…
- http://msdn.microsoft.com…
- http://mikehadlow.blogspot.com…
- http://www.codeproject.com…
Downloads:
Viel Spaß beim entwickeln : )


