Global value from login

  • Thread starter Thread starter larzeb
  • Start date Start date
L

larzeb

The application requires that a user login, part of which is to
identify the company he wishes to work on.

The company field is used throughout the application as a SQL
selection field used in many queries. There are many modules which
need access to this company value.

Where is the appropriate place to store this value so that all the
other modules can access it?

TIA, Lars
 
Hello larzeb,

Ive encountered a similar situation in an ASP.NET application, and I dont
believe the solution is that far off for a winforms app.

My solution involved deriving from GenericIdentity and adding a company to
the class.

public class MyIdentity : GenericIdentity
{
private readonly string company;

public MyIdentity(string username, string company) : base(username)
{
this.company = company;
}

public string Company { get { return company; } }
}

Now, pass an instance of this class off to GenericPrincipal and set the thread
principal in your validation method like this:

string[] roles = someMethodToGetTheRoles();
IIdentity identity = new MyIdentity(username, company);
IPrincipal principal = new GenericPrincipal(identity, roles);

AppDomain.CurrentDomain.SetThreadPrincipal(principal);

Later, when you need to check the company, use

MyIdentity identity = (MyIdentity)Thread.CurrentPrincipal.Identity;
 
Hi Lars,

I think this is a wide topic. This is based on what type of application you
created, and which architecture your application used.

I think your request is somewhat like: how to achieve the Asp.net Forms
authentication in Winform app. Matt's solution is wrapping the company
information in GenericIdentity object, then store it through
AppDomain.CurrentDomain.SetThreadPrincipal, normally, if your application
is single thread, this may meet your need. But if your application is
multi-threaded, and the module in another thread can not access this
Generic identity in appDomain's main thread.

I think another possible solution may just create a tool class, which we
can use a static field of this class to store the company information, and
all the modules in the Application can access this tool class's static
field without problem.

Hope this helps.
=================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Jeffrey,

Forgive my ignorance, but if I define a class with static fields, what
is the lifetime of that object in a multi-project application? Will it
be available for the life of the entire application?

Thanks, Lars
 
Hi Lars,

Thanks very much for your feedback!

Normally, the static member's lifetime is as long as the class's(not the
instance's) lifetime. So the lifetime is as long as the AppDomain.

So if any module can access this static member in AppDoamain, the static
member is valid.

Hope this information helps.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top