Solved

C# - Impersonate Context Question

Posted on 2009-05-04
1
809 Views
Last Modified: 2012-05-06
I have a console application where I have to use impersonate to access a network directory.  I then have code that loops though all the files in a directory on that network server.  After a few minutes sometimes not all it throws a System.IO.Exception - the specified network path is no longer available.  Any idea if this is possibly a impersonate issue and what I can change?  Below is the code in an impersonate.cs class I have that I call first before running the code and here is the exact stack trace:

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)\r\n   at System.IO.Directory.InternalGetFileDirectoryNames(String path, String userPathOriginal, String searchPattern, Boolean includeFiles, Boolean includeDirs, SearchOption searchOption)\r\n   at System.IO.DirectoryInfo.GetFiles(String searchPattern, SearchOption searchOption)\r\n   at System.IO.DirectoryInfo.GetFiles(String searchPattern)\r\n   at BillRunPowerCleanup.Proram.CleanupMacOldPDFs() in C:\\Applications\\BillRunPower\\BillRunPowerCleanup\\BillRunPowerCleanup\\Program.cs:line 56"
public class Impersonator

    {

        public WindowsImpersonationContext impersonationContext;

        

        [DllImport("advapi32.dll")]

        public static extern int LogonUser(String lpszUsername, String lpszDomain,

        String lpszPassword,

        int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
 

        [DllImport("kernel32.dll")]

        public extern static bool CloseHandle(IntPtr hToken);
 

        public bool Impersonate(string userName, string domain, string password)

        {

        WindowsIdentity tempWindowsIdentity;

        IntPtr token = IntPtr.Zero;

        IntPtr tokenDuplicate = IntPtr.Zero;

        // request default security provider a logon token with LOGON32_LOGON_NEW_CREDENTIALS

        // token returned is impersonation token, no need to duplicate

        if(LogonUser(userName, domain, password, 9, 0, ref token) != 0)

        {

        tempWindowsIdentity = new WindowsIdentity(token);

        impersonationContext = tempWindowsIdentity.Impersonate();

        // close impersonation token, no longer needed

        CloseHandle(token);

        if (impersonationContext != null)

        return true;

        }

        return false; // Failed to impersonate.

        }
 

    }

Open in new window

0
Comment
Question by:sbornstein2
1 Comment
 
LVL 13

Accepted Solution

by:
kaylanreilor earned 500 total points
ID: 24351599
I don't see the connection with an impersonation issue. The impersonation will help to be allowed to access the shared folder.
To test it you should remove the impersonation code a set a permission on the folder to everyone. Thus I guess you will experience the same problem.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now