Solved

C# - Impersonate Context Question

Posted on 2009-05-04
1
818 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Comparing String Arrays. 5 57
Graphics 2 32
Add different cell to otherwise similiar row 4 45
.NET 2008 VB and C# 6 39
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

789 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