Groups | Blog | Home
all groups > dotnet web services enhancements > january 2005 >

dotnet web services enhancements : Thread.CurrentPrincipal is emtpy!


Scribnar
1/17/2005 9:11:03 PM
Hi,

I set the token.Principal and Thread.CurrentPrincipal to CustomPrincipal in
my CustomUsernameTokenManager.AuthenticateToken method. When i try accessing
the same in my WebMethod, i'm able to access the CustomPrincipal only through
the token.Principal variable. When i try with Thread.CurrentPrincipal, it
does not have the CustomPrincipal object in it because of which username and
the roles list is empty.

One might argue why i need to try this. The Business components i have
written takes the logged in users name and role list from the
Thread.CurrentPrincipal so what ever type of application is using the
Business component just need to set the Thread.CurrentPrincipal. I would not
want set toekn.Principal instead Thread.CurrentPrincipal. Does anyone know
why Thread.CurrentPrincipal is not giving my CustomPrincipal that i set in
AuthenticateToken method?

Thanks in Advance!
--
Regards,
Dilip Krishnan
1/17/2005 9:33:31 PM
Hello Scribnar,
Could you post your code please?

HTH
Regards,
Dilip Krishnan
MCAD, MCSD.net
dkrishnan at geniant dot com
http://www.geniant.com

[quoted text, click to view]

Scribnar
1/17/2005 10:35:02 PM
Hi Dilip,

Thanks for your replies. Please find the code below.


protected override string AuthenticateToken( UsernameToken token )
{

if(!obj.ValidateUser(token.Username, token.Password))
{
//return any invalid password which will throw authendication failed
message.
return DateTime.Now.ToLongDateString();
}
else
{
IIdentity id = new GenericIdentity(token.Username);
//This line creates a principal for the given identity with the list of
roles and returns the principal after setting Thread.CurrentPrincipal to the
principal object created
token.Principal = CustomSecurity.AttachPrincipal(id);
Thread.CurrentPrincipal = token.Principal;
return token.Password;
}

}

I have created a class CustomPrincipal extending IPrincipal interface. That
is being returned from CustomSecurity.AttachPrincipal method.

Following is the simple version of my web method.
[WebMethod]
public string RetriveAllAppliedBuilds()
{
string userName = System.Threading.Thread.CurrentPrincipal.Identity.Name;

return userName ;
}

Thanks.

Regards,
-Suresh.


[quoted text, click to view]
Scribnar
1/17/2005 10:59:03 PM
Hi William,

Does that mean thread pooling happens in ASP.NET as well?

Thanks.
-Suresh.

[quoted text, click to view]
Scribnar
1/17/2005 11:05:02 PM
William,

Sorry that would stupid question as its obvious in the <processModel>
element in machine.config.

-Suresh.

[quoted text, click to view]
William Stacey [MVP]
1/18/2005 12:41:00 AM
I blogged a sample doing this and it worked for me.
http://spaces.msn.com/members/staceyw/Blog/cns!1pnsZpX0fPvDxLKC6rAAhLsQ!166.entry

The one thing to watch for is clear Thread.CurrentPrincipal before the
WebMethod returns as it is a ThreadPool thread - as it may/will get reused.
So you don't want another thread using your last principal object for code
access security.

--
William Stacey, MVP
http://mvp.support.microsoft.com

[quoted text, click to view]
AddThis Social Bookmark Button