Click here to Skip to main content
15,887,214 members
Home / Discussions / C#
   

C#

 
GeneralImpersonation and FileSystemWatcher (threading?) Pin
Bill Dean5-Feb-04 5:47
Bill Dean5-Feb-04 5:47 
GeneralRe: Impersonation and FileSystemWatcher (threading?) Pin
John Fisher5-Feb-04 6:00
John Fisher5-Feb-04 6:00 
GeneralRe: Impersonation and FileSystemWatcher (threading?) Pin
Bill Dean5-Feb-04 6:08
Bill Dean5-Feb-04 6:08 
GeneralRe: Impersonation and FileSystemWatcher (threading?) Pin
Heath Stewart5-Feb-04 6:29
protectorHeath Stewart5-Feb-04 6:29 
GeneralRe: Impersonation and FileSystemWatcher (threading?) Pin
Bill Dean5-Feb-04 6:54
Bill Dean5-Feb-04 6:54 
GeneralRe: Impersonation and FileSystemWatcher (threading?) Pin
Heath Stewart5-Feb-04 8:51
protectorHeath Stewart5-Feb-04 8:51 
GeneralRe: Impersonation and FileSystemWatcher (threading?) Pin
Bill Dean5-Feb-04 9:26
Bill Dean5-Feb-04 9:26 
GeneralRe: Impersonation and FileSystemWatcher (threading?) Pin
Bill Dean5-Feb-04 16:00
Bill Dean5-Feb-04 16:00 
OK...

I could not get the AppDomain.SetThreadPrincipal to work, so I took another tact.



I created a class ImpersonatingLurker that derives from the System.IO.FileSystemWatcher. The only difference is that the first thing it does in it's constructor is the impersonation. This seems the get the principal set correctly for the thread then handles the FileSystemWatcher. The full class definition is:

public class ImpersonateingLurker :System.IO.FileSystemWatcher
{

     public ImpersonateingLurker(string strPath,string strFilter, System.Security.Principal.WindowsPrincipal  p)
		{
			Debug.WriteLine ("Lurker Constructor:"+p.Identity.Name );

			WindowsIdentity i = (WindowsIdentity) p.Identity;
			i.Impersonate ();

			this.Path = strPath;
			this.Filter = strFilter;
		}

}


Then in the OnStart event handler for the windows service we do:

int iToken;
LogonUser("user","domain","password",3,0,out iToken);

IntPtr token2 = new IntPtr(iToken);
WindowsIdentity wi  = new WindowsIdentity(token2);
WindowsPrincipal wp = new WindowsPrincipal (wi);

...

ImpersonateingLurker IL = new ImpersonateingLurker (PathOfInterest,Filter,wp);


And it all seems to work out...Thanks to Heath and John for their advice!

Bill
GeneralComboBox in Grid Pin
H r M5-Feb-04 5:03
H r M5-Feb-04 5:03 
GeneralRe: ComboBox in Grid Pin
John Fisher5-Feb-04 5:29
John Fisher5-Feb-04 5:29 
GeneralObtaining VBA stored BLOB Values from a Database Pin
Paul Kennedy5-Feb-04 5:01
Paul Kennedy5-Feb-04 5:01 
GeneralRe: Obtaining VBA stored BLOB Values from a Database Pin
John Fisher5-Feb-04 5:40
John Fisher5-Feb-04 5:40 
GeneralRe: Obtaining VBA stored BLOB Values from a Database Pin
Heath Stewart5-Feb-04 6:14
protectorHeath Stewart5-Feb-04 6:14 
GeneralRegular Expression Help Pin
David Flores5-Feb-04 4:35
David Flores5-Feb-04 4:35 
GeneralRe: Regular Expression Help Pin
John Fisher5-Feb-04 5:50
John Fisher5-Feb-04 5:50 
GeneralRe: Regular Expression Help Pin
David Flores5-Feb-04 7:03
David Flores5-Feb-04 7:03 
Generalpdf 2 txt Pin
Mahesh Varma5-Feb-04 4:04
Mahesh Varma5-Feb-04 4:04 
GeneralRe: pdf 2 txt Pin
Heath Stewart5-Feb-04 6:07
protectorHeath Stewart5-Feb-04 6:07 
GeneralRe: pdf 2 txt Pin
Daniel Turini5-Feb-04 8:56
Daniel Turini5-Feb-04 8:56 
GeneralRe: Challenge: Generating Signed Serial Numbers Pin
Heath Stewart5-Feb-04 5:40
protectorHeath Stewart5-Feb-04 5:40 
GeneralRe: Challenge: Generating Signed Serial Numbers Pin
Heath Stewart5-Feb-04 8:39
protectorHeath Stewart5-Feb-04 8:39 
GeneralRe: Challenge: Generating Signed Serial Numbers Pin
Heath Stewart5-Feb-04 11:24
protectorHeath Stewart5-Feb-04 11:24 
GeneralRe: Challenge: Generating Signed Serial Numbers Pin
Heath Stewart6-Feb-04 1:16
protectorHeath Stewart6-Feb-04 1:16 
QuestionHow to implement "Open With Dialog Box" in .NET Pin
Shailaja5-Feb-04 3:08
Shailaja5-Feb-04 3:08 
AnswerRe: How to implement "Open With Dialog Box" in .NET Pin
Heath Stewart5-Feb-04 5:53
protectorHeath Stewart5-Feb-04 5:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.