storing user id in context

  • Thread starter Thread starter Andrey P
  • Start date Start date
A

Andrey P

We are trying to develop a framework and are not planning to use windows
security. Is there a way to store a user name in some kind of 'context'
associated with current thread so any part of the framework that we'll
develop can access user name. There is a Thread.CurrentContext, however
..NET says we should not use that class. Any other way to do that?

thanks
Andrey
 
You can define a static variable and mark it with the ThreadStatic
attribute.
In C#, you would write something like:

[ThreadStatic]
private static string _userName;

/* expose it as static property */
public static string UserName { get { return _userName; } set { _userName =
value; } }

The ThreadStatic attribute guarantees that every thread will have its own
copy of the variable.

Bruno.
 
There is a CallContext class in System.Runtime.Remoting.Messaging. It has
static GetData and SetData methods to store name/value pairs. So you can
set an object in one part of your program, and retrieve it at another.
 
Back
Top