Quick WCF Authentication Howto

Posted on: Wed Sep 02 15:49:45 -0700 2009. Updated on: Wed Sep 02 16:01:44 -0700 2009.
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.

Comments:

Mon Sep 21 22:35:42 -0700 2009
How to retrun vendor object ?


---------------------------------------------------

Add a Comment:

simple_captcha.jpg
(human authentication)


Ads

Categories

About

Random foliage

This website is meant to be a reference for ASP Dot Net developers. The entries are a compilation of things I've figured out how to do and that I deem useful to keep of track for future reference. Assumptions: web development with: C Sharp (vb sucks), visual studio 05/08, .net 3.5, meant for programmers. Written by: James Reategui.