How to get the current principle object within a windows service.

How to get the current principle object within a windows service.

Old forum URL: forums.lhotka.net/forums/t/8682.aspx


Stephen K posted on Thursday, March 18, 2010

Hi all.

I need to get the current principle object that a windows service is executing under.  I would like to use the AD account to generate a Csla principal object to work with my business object library.  I thought that System.Threading.Thread.CurrentPrincipal would be it, but it seems that it is nothing in the OnStart procedure.  The service is running on a Windows Server 2003 R2.

Any thoughts would be greatly appreciated.

Thanks-

Stephen K.

ajj3085 replied on Thursday, March 18, 2010

To get the current principal of the user account which is running any application, you need to do this:

var prin = new WindowsPrincipal( WindowsIdentity.GetCurrent() );

By default a Thread will have the GenericPrincipal set.  You can also modify this (as the first lines of code your execute on startup) using AppDomain.SetPrincipalPolicy or AppDomain.SetThreadPrincipal, depending on what you need to do.

HTH

Stephen K replied on Thursday, March 18, 2010

Andy - thanks for the clairification.

Stephen K.

Copyright (c) Marimer LLC