I have a working custom UserNamePasswordValidator that calls into my Oracle DB.
This class derives from System.IdentityModel.Selectors.UserNamePasswordValidator and the Validate() method returns void.
I load my User object from the database, and once the password is validated, I want to stash my “User” object so the service can access it when going about its business. In ASP.NET / Java land I would stash it into a session, or perhaps my overall Controller class. How do I do this from the Validator in WCF?
Or, in other words, what is the best practice in WCF land to set a custom User domain object for the service.
Update: This is how I’ve worked around it. I cache the User object during the validator, then access it later in the AuthorizatinPolicy step.
// this gets called after the custom authentication step where we loaded the User
public bool Evaluate(EvaluationContext evaluationContext, ref object state)
{
// get the authenticated client identity
IIdentity client = GetClientIdentity(evaluationContext);
User user;
OraclePasswordValidator.users.TryGetValue(client.Name, out user);
if(user != null) {
// set the custom principal
evaluationContext.Properties["Principal"] = user;
return true;
}
return false;
}
I’m not a WCF expert, but from what I’ve read and implemented so far, the ‘correct’ way to do this would be to use the
Validatorto authenticate the user, and then implement anIAuthorizationPolicyto do the actual authorization. So it would be in the authorization policy that you’ll set your custom principal on the current thread.To be able to forward information from the username/password validation, you can implement a security token authenticator that inherits from
UserNameSecurityTokenAuthenticator. The SecurityTokenAuthenticator will first call the validator and if validation succeeds, it can add your custom authorization policy and send userinfo to the policy through the constructor. Something a long the lines of this:There’s an article here that describes a bit more around the involved classes; http://blogs.msdn.com/card/archive/2007/10/04/how-identity-providers-can-show-custom-error-messages-in-cardspace.aspx