English 中文(简体)
ASP.net web services
原标题:

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

问题回答

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.

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.

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签