If you want to use SSL, you will need to use X509 certificates.
If you are going to host it in IIS and enable SSL, you need to provide a certificate, for debugging purposes you can create a self-signed certificate from IIS.
After you configure it in IIS, you will need to edit the WCF binding to enable SSL.
You will need a binding with the security mode transport set
<basicHttpBinding> <binding name="SecureBinding" receiveTimeout="01:00:00"> <security mode="Transport" /> </binding> </basicHttpBinding>
and safe behavior, the following indicates the ssl certificate to be used.
<behaviors> <serviceBehaviors> <behavior name="SecureBehavior"> <serviceMetadata /> <serviceCredentials> <serviceCertificate findValue="localhost" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName" /> </serviceCredentials> </behavior> <behavior name="StandardBehavior"> </behavior> </serviceBehaviors> </behaviors>
you need to create a secure endpoint
<services> <service behaviorConfiguration="SecureBehavior" name="secureService"> <endpoint address="" binding="basicHttpBinding" bindingConfiguration="SecureBinding" contract="<Your webservice class name including namespace>" /> <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" /> </service>
This will allow you to use SSL on your site after it is hosted.
To connect from your client, you should use the following (assuming C #)
BasicHttpBinding binding = new BasicHttpBinding(); binding.Security.Mode = BasicHttpSecurityMode.Transport; EndPointAddress endpointAddress = new EndpointAddress("Your Service address (including HTTPS)"); Client svc = new Client(binding,endpointAddress)
Another method would be to use encryption instead of SSL. Encrypt the password on the client side and send the encrypted data to the service, I'm not sure of the best practice for this.
Hope this helps
EDIT
If you want to send the username and password to the service, you just need to create a new method inside the service.
In the interface file (IService1.cs) you define an operation contract
[OperationContract] bool Login(string password,string username);
then you must create a method in the service class (Service1.svc)
public bool Login(string password,string username) {
This is probably the easiest way to do this. Another tricky way would be to use a custom membership provider to authenticate users.
You will need to create a class that inherits from MembershipProvider and override the ValidateUser method
public class SampleMembershipProvider : MembershipProvider { public override bool ValidateUser(string username, string password) {
now you need to tell webconfig about the use of forms authentication and use the custom member role provider
<authentication mode="Forms" /> <membership defaultProvider="CustomMembershipProvider"> <providers> <clear /> <add name="CustomMembershipProvider" type="SampleApplication.CustomMembershipProvider" /> </providers> </membership>
Now that you have your member set, you can change your login code from the top to the following code, this code will authenticate the user and set an authorization cookie.
public bool Login(string password,string username) { if (Membership.ValidateUser(username, password)) { FormsAuthentication.SetAuthCookie(username, false); return true; } return false; }
Now, when you call the method on your service, you can check if the user authentication has passed, and if you can run this command, otherwise not.
bool DoWork() { if (HttpContext.Current.User.Identity.IsAuthenticated) { //do something return true; } else { return false; } }
Let me know if you need me to clarify anything.