.NET Framework Developer Center > .NET Development Forums > Windows Communication Foundation > When using Custom Username Authentication, is it possible to set Thread.CurrentPrincipal.Identity?
Ask a questionAsk a question
 

QuestionWhen using Custom Username Authentication, is it possible to set Thread.CurrentPrincipal.Identity?

  • Wednesday, June 24, 2009 1:32 PMChris Bardon Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    I'm experimenting with a custom UserName validator, and while it seems to work, I've noticed that even when the credentials are passed to the service and validated, other service methods treat the client as unauthenticated.  For example, if I have something like this as the validator:

            public override void Validate(string userName,
                string password)
            {
                // Validate arguments
                if (userName == null)
                    throw new ArgumentNullException("userName");
                if (password == null)
                    throw new ArgumentNullException("password");
    
                // Validate username and password
                if (userName != "user" || password != "pass")
                {
                    throw new SecurityTokenException(
                        "Invalid username or password.");
                }
            }
    
    The method works as you'd expect.  When I try to do this in the service method though:
    string name = Thread.CurrentPrincipal.Identity.Name;
    bool IsAuthenticated = Thread.CurrentPrincipal.Identity.IsAuthenticated;
    
    The CurrentPrincipal identifies itself as a WindowsPrincipal, the Name is empty, and IsAuthenticated is false.  I already passed a custom validation though, so why should this be the case?  Is there a way to store the validated credentials in the Validate() method?  Here's what I have in the config for the custom authentication:

              <serviceCredentials>
                <serviceCertificate findValue="chrislaptop2"
                        storeLocation="CurrentUser" storeName="My"
                                    x509FindType="FindBySubjectName"/>
                <userNameAuthentication userNamePasswordValidationMode="Custom"
                        customUserNamePasswordValidatorType="TestAuthService.TestAuth, TestAuthService"/>
              </serviceCredentials>
    And for the binding:
        <bindings>
          <wsHttpBinding>
            <binding name="UserNameWS">
              <security mode="Message">
                <transport clientCredentialType="None"/>
                <message clientCredentialType="UserName"/>
              </security>
            </binding>
          </wsHttpBinding>
        </bindings>
    
    Is there anything else that I'm missing here, or is there a better way to implement custom validation that I'm missing?

All Replies