views:

81

answers:

3

I am using a web service which sets the Thread.CurrentPrincipal object while logging in and soon later when another webmethod of the same web service accesses Thread.CurrentPrincipal, its different/resets

Can someone tell me if this is expected or can different webmethod calls from the same client can access the same Thread.CurrentPrincipal object

Thanks

+1  A: 

As soon as you stop using a thread it goes back into the thread pool.

The next call will take a thread from the thread pool, but you have no control which one you get.

You need to send information about which user is making the call, with each request.

Shiraz Bhaiji
Thanks for the quick response, I did have a hunch about that, if its not too much could you help by giving/showing an example
Sloane
or If I could use a Session variable
Sloane
A: 

This is expected, every new web request is actually new thread. And every web request reset stuff like CurrentThread, CurrentCulture and so on.

What are you trying to do is authentication session. There are many possible solutions. But to suggest something I have to specify technology you use.

For example, ASP.NET ASMX Services can use Forms Authentication. Also they are aware about ASP.NET Session.

With WCF, you can enable ASP.NET support, so you will have same stuff like for ASP.NET ASMX Services. But you also can leverage on Windows Communication Foundation Authentication Service.

Anyways need more info from you.

Mike Chaliy
I am currently using a Windows form application which will consume the web service(s) for various operations. By the information in the comment by Shiraz I suppose I would have to implement all methods similar to an API which would accept username/password with each request.Or I would be glad to read a workaround
Sloane
web service = this is ASP.NET ASMX Service? Or WCF Service?
Mike Chaliy
yes I was referring to asp.net asmx Service
Sloane
A: 

If you are using the built-in ASP .NET authentication for your website and then just calling the web service from a web page, you may be able to enable session variables and user context information in the methods of the web service with a decoration. Like this:

[WebMethod(EnableSession=true)]
public void MyWebMethod()
{
    string mySessionVar = HttpContext.Current.Session["sessionVar"].ToString();
    IPrincipal currentUser = HttpContext.Current.User;
    ...
}

If that doesn't solve your problem, tell us what are you using the Thread.CurrentPrincipal object for (what you are pulling out of the Thread.CurrentPrincipal object). Perhaps there is another solution.

Kasey Speakman