Quick WCF Authentication Howto
Category: DotNet
Getting WCF authentication working is not to hard once you know where things go.
Start off by creating a new WCF Service Application. Add a class like the following:
public class CustomUserNameValidator : UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
if (null == userName || null == password)
{
throw new ArgumentNullException();
}
// validate username and pass
DataModelUsersDataContext dc = new DataModelUsersDataContext();
Vendor vendor = dc.Vendors.FirstOrDefault(t => t.ServiceUsername == userName && t.ServicePassword == password);
if (vendor == null)
{
throw new FaultException(new SecurityTokenException("user or pwd incorrect"), "user or pwd incorrect.");
}
}
}
Not how it inherits from that validator class. It will automatically pass in the username and password on validate, then you can figure out what to do with. Throw an exception if fails validation.
Now have to tell your service to use this custom class. Do this in the web.config:
...
Note the serviceCredentials entry. That is where we set it to use a custom validator mode and specify the class to use. Also note the serviceCertificate entry. You must use securtiy so be sure to specify a cert. This cert can be self-signed and must be kept in your certificate store on your localmachine. If you are not sure how to register your cert on your localmachine or server there is plenty of documentation online.
---------------------------------------------------