1

Im have a custom validator in my WCF service application where i validate the username and password with the standard Microsoft identity tables.

This function returns me data form the user including the UserID (Guid)

 User user = new IdentityAuthenticator().ValidateUser(userName, password);

now i want to add the UserID in the operationContext so i can user the UserID later in som other functions

OperationContext.Current.ServiceSecurityContext.AuthorizationContext.Properties["UserID"] = user.UserID;

but when i do that i get the error message "Object reference not set to an instance of an object." what is not surprising because i did not create an instance of it. but i don't know how

So my question is how can i save the UserID in the OperationContext from my Validate function so that i can reuse the userID in other functions.

namespace Facilicom.SOA.Service.FSE.Implementation.Authentication

{

public class IdentityValidator : UserNamePasswordValidator
{

public override void Validate(string userName, string password)

    {

        Debug.WriteLine("Check user name");
        User user = new IdentityAuthenticator().ValidateUser(userName, password);
        if (user != null)
            OperationContext.Current.ServiceSecurityContext.AuthorizationContext.Properties["UserID"] = user.UserID;
        else
        {
            var msg = String.Format("You are not authorized");
            Trace.TraceWarning(msg);
            throw new FaultException(msg);//the client actually will receive MessageSecurityException. But if I throw MessageSecurityException, the runtime will give FaultException to client without clear message.
        }
       }
    } 
}
user2811133
  • 69
  • 2
  • 7

0 Answers0