====== WCF - Secured service ====== * [[http://msdn.microsoft.com/en-us/library/bb386582.aspx|WCF Authentification overview on MSDN]] * This is short description how make secured wcf service with sql membership and role provider. ===== Server side ===== ** web.config:** **service class:** public class Service: IService { [PrincipalPermission(SecurityAction.Demand, Role = "User")] public string Operation(string text) { return text; } ... } ==== Security ==== * On server side must be installed "Client Certificate Mapping Authentication" in services in IIS server role. {{:programming:csharp:wcf:selectserverrole_security_certificate.png|}} ===== Certificates ===== * generate certificate makecert.exe -sr LocalMachine –pe -ss My -a sha1 -n CN=MyServerCert -sky exchange MyServerCert.cer [[http://msdn.microsoft.com/en-us/library/bfsktky3%28VS.80%29.aspx|Makecert.exe]] Generating with makecert invoke some issues, for me is beter following application {{:programming:csharp:wcf:selfcert.zip|}} {{:programming:csharp:wcf:selfcert.png|}} ==== Install certifikates ==== * **On server side.** Add certificate for "Local machine", certicate must be trusted, so if is self signed, add to "Trusted root certification authorities". * **On client side.** If you use self signed certificate, you have to manualy add certificate to Trusted people. RUN > MMC > File > Add/Remove snap-in > Computer Account > Cerficate. ===== Client side ===== * client program using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WCFconsumer { class Program { static void Main(string[] args) { ServiceClient client = new ServiceClient(); client.ClientCredentials.UserName.UserName = "User"; client.ClientCredentials.UserName.Password = "Passwd"; client.Operation("test"); client.Close(); } } } * app.config ===== Membership and role provider ===== * [[http://msdn.microsoft.com/en-us/library/ms731049.aspx|Membership provider on MSDN]] * [[http://msdn.microsoft.com/en-us/library/aa702542.aspx|Role provider on MSDN]] ===== Resources ===== * [[http://codebetter.com/petervanooijen/2010/03/22/a-simple-wcf-service-with-username-password-authentication-the-things-they-don-t-tell-you/]] * [[http://www.devatwork.nl/2007/05/wcf-username-authentication/]] * [[http://social.msdn.microsoft.com/Forums/en/windowscardspace/thread/057b472b-bb49-4a7a-873c-cb41adbb8298]] * [[http://www.netframeworkdev.com/windows-communication-foundation/certificate-for-username-authentication-in-wcf-58465.shtml]]