When using Custom Username Authentication, is it possible to set Thread.CurrentPrincipal.Identity?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:<br/> <br/> <pre lang="x-c#"> public override void Validate(string userName, string password) { // Validate arguments if (userName == null) throw new ArgumentNullException(&quot;userName&quot;); if (password == null) throw new ArgumentNullException(&quot;password&quot;); // Validate username and password if (userName != &quot;user&quot; || password != &quot;pass&quot;) { throw new SecurityTokenException( &quot;Invalid username or password.&quot;); } }</pre> The method works as you'd expect.  When I try to do this in the service method though:<br/> <pre lang="x-c#">string name = Thread.CurrentPrincipal.Identity.Name; bool IsAuthenticated = Thread.CurrentPrincipal.Identity.IsAuthenticated;</pre> 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:<br/> <br/> <pre> &lt;serviceCredentials&gt; &lt;serviceCertificate findValue=&quot;chrislaptop2&quot; storeLocation=&quot;CurrentUser&quot; storeName=&quot;My&quot; x509FindType=&quot;FindBySubjectName&quot;/&gt; &lt;userNameAuthentication userNamePasswordValidationMode=&quot;Custom&quot; customUserNamePasswordValidatorType=&quot;TestAuthService.TestAuth, TestAuthService&quot;/&gt; &lt;/serviceCredentials&gt;</pre> And for the binding:<br/> <pre lang=x-xml> &lt;bindings&gt; &lt;wsHttpBinding&gt; &lt;binding name=&quot;UserNameWS&quot;&gt; &lt;security mode=&quot;Message&quot;&gt; &lt;transport clientCredentialType=&quot;None&quot;/&gt; &lt;message clientCredentialType=&quot;UserName&quot;/&gt; &lt;/security&gt; &lt;/binding&gt; &lt;/wsHttpBinding&gt; &lt;/bindings&gt;</pre> Is there anything else that I'm missing here, or is there a better way to implement custom validation that I'm missing?© 2009 Microsoft Corporation. All rights reserved.Sun, 05 Jul 2009 00:15:11 Zf948d19c-d46c-4776-a22e-c0b8e756847ehttp://social.msdn.microsoft.com/Forums/en-US/wcf/thread/f948d19c-d46c-4776-a22e-c0b8e756847e#f948d19c-d46c-4776-a22e-c0b8e756847ehttp://social.msdn.microsoft.com/Forums/en-US/wcf/thread/f948d19c-d46c-4776-a22e-c0b8e756847e#f948d19c-d46c-4776-a22e-c0b8e756847eChris Bardonhttp://social.msdn.microsoft.com/Profile/en-US/?user=Chris%20BardonWhen using Custom Username Authentication, is it possible to set Thread.CurrentPrincipal.Identity?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:<br/> <br/> <pre lang="x-c#"> public override void Validate(string userName, string password) { // Validate arguments if (userName == null) throw new ArgumentNullException(&quot;userName&quot;); if (password == null) throw new ArgumentNullException(&quot;password&quot;); // Validate username and password if (userName != &quot;user&quot; || password != &quot;pass&quot;) { throw new SecurityTokenException( &quot;Invalid username or password.&quot;); } }</pre> The method works as you'd expect.  When I try to do this in the service method though:<br/> <pre lang="x-c#">string name = Thread.CurrentPrincipal.Identity.Name; bool IsAuthenticated = Thread.CurrentPrincipal.Identity.IsAuthenticated;</pre> 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:<br/> <br/> <pre> &lt;serviceCredentials&gt; &lt;serviceCertificate findValue=&quot;chrislaptop2&quot; storeLocation=&quot;CurrentUser&quot; storeName=&quot;My&quot; x509FindType=&quot;FindBySubjectName&quot;/&gt; &lt;userNameAuthentication userNamePasswordValidationMode=&quot;Custom&quot; customUserNamePasswordValidatorType=&quot;TestAuthService.TestAuth, TestAuthService&quot;/&gt; &lt;/serviceCredentials&gt;</pre> And for the binding:<br/> <pre lang=x-xml> &lt;bindings&gt; &lt;wsHttpBinding&gt; &lt;binding name=&quot;UserNameWS&quot;&gt; &lt;security mode=&quot;Message&quot;&gt; &lt;transport clientCredentialType=&quot;None&quot;/&gt; &lt;message clientCredentialType=&quot;UserName&quot;/&gt; &lt;/security&gt; &lt;/binding&gt; &lt;/wsHttpBinding&gt; &lt;/bindings&gt;</pre> Is there anything else that I'm missing here, or is there a better way to implement custom validation that I'm missing?Wed, 24 Jun 2009 13:32:50 Z2009-06-24T13:32:50Zhttp://social.msdn.microsoft.com/Forums/en-US/wcf/thread/f948d19c-d46c-4776-a22e-c0b8e756847e#82d7285a-f31f-498c-9770-89f753968403http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/f948d19c-d46c-4776-a22e-c0b8e756847e#82d7285a-f31f-498c-9770-89f753968403Lars Wilhelmsenhttp://social.msdn.microsoft.com/Profile/en-US/?user=Lars%20WilhelmsenWhen using Custom Username Authentication, is it possible to set Thread.CurrentPrincipal.Identity?Hi,<br/> <br/>  I think you can simply assign a new instance of GenericPrincipal / GenericIdentity to the CurrentPrincipal in your Validate method,<br/>  and later you will be able to use .IsAuthenticated and the other methods of the IPrincipal interface.<br/> <br/>  --larsw<br/><hr class="sig">Lars Wilhelmsen | Senior Consultant | <a href="http://www.miles.no/">Miles</a>, Norway | <a href="https://mvp.support.microsoft.com/profile/Lars">Connected Systems MVP</a> | <a href="http://larswilhelmsen.com/">http://larswilhelmsen.com/</a>Fri, 03 Jul 2009 21:12:35 Z2009-07-03T21:12:35Zhttp://social.msdn.microsoft.com/Forums/en-US/wcf/thread/f948d19c-d46c-4776-a22e-c0b8e756847e#4f745c49-4ce0-458c-adae-22fd4e418177http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/f948d19c-d46c-4776-a22e-c0b8e756847e#4f745c49-4ce0-458c-adae-22fd4e418177Pedro Felixhttp://social.msdn.microsoft.com/Profile/en-US/?user=Pedro%20FelixWhen using Custom Username Authentication, is it possible to set Thread.CurrentPrincipal.Identity?Hi, <br/> <br/> Using &quot;plain&quot; WCF, see <a title="http://msdn.microsoft.com/en-us/library/aa702720.aspx" href="http://msdn.microsoft.com/en-us/library/aa702720.aspx" title="http://msdn.microsoft.com/en-us/library/aa702720.aspx">http://msdn.microsoft.com/en-us/library/aa702720.aspx</a> or <a title="http://www.leastprivilege.com/CustomPrincipalsAndWCF.aspx" href="http://www.leastprivilege.com/CustomPrincipalsAndWCF.aspx" title="http://www.leastprivilege.com/CustomPrincipalsAndWCF.aspx">http://www.leastprivilege.com/CustomPrincipalsAndWCF.aspx</a> <br/> Basically, you have to create a custom authorization policy (<strong>IAuthorizationPolicy</strong> ) and set the &quot;Principal&quot; property of the evaluation context.<br/> <br/> If you are using WCF+Geneva FX, then you should define a custom <strong>ClaimsAuthenticationManager</strong> .<br/> <br/> HTH<br/> Pedro Felix<hr class="sig">http://pfelix.wordpress.comSun, 05 Jul 2009 00:15:10 Z2009-07-05T00:15:10Z