Microsoft 开发人员网络 > 论坛主页 > Windows Communication Foundation > When using Custom Username Authentication, is it possible to set Thread.CurrentPrincipal.Identity?
提出问题提出问题
 

问题When using Custom Username Authentication, is it possible to set Thread.CurrentPrincipal.Identity?

  • 2009年6月24日 13:32Chris Bardon 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     包含代码
    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?

全部回复