0

How can I use .Net to open a file with specific user.
Example:

File.OpenFile(filePath, user)
cuongle
  • 74,024
  • 28
  • 151
  • 206
R.Kaka
  • 167
  • 6
  • 16
  • 1
    Please clarify: Do you want to open the file using the credentials of a specific user? – Brian Rasmussen Feb 22 '13 at 04:41
  • This method doesn't exist in the .NET framework. Be more specific in what you want to accomplish, not the method you want to use. – Rich Feb 22 '13 at 04:42
  • I just would like to check whether user can open/read a file or not. (any users not only log on user) – R.Kaka Feb 22 '13 at 06:12

1 Answers1

0

from this thread:

const string file = @"\\machine\test\file.txt";

        using (UserImpersonation2 user = new UserImpersonation2("user", "domain", "password"))
        {
            if (user.ImpersonateValidUser())
            {
                StreamReader reader = new StreamReader(file);
                Console.WriteLine(reader.ReadToEnd());
                reader.Close();
            }
        }


class UserImpersonation2:IDisposable
{
    [DllImport("advapi32.dll")]
    public static extern bool LogonUser(String lpszUserName,
        String lpszDomain,
        String lpszPassword,
        int dwLogonType,
        int dwLogonProvider,
        ref IntPtr phToken);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    public static extern bool CloseHandle(IntPtr handle);

    WindowsImpersonationContext wic;
    IntPtr tokenHandle;
    string _userName;
    string _domain;
    string _passWord;

    public UserImpersonation2(string userName, string domain, string passWord)
    {
        _userName = userName;
        _domain = domain;
        _passWord = passWord;
    }

    const int LOGON32_PROVIDER_DEFAULT = 0;
    const int LOGON32_LOGON_INTERACTIVE = 2;

    public bool ImpersonateValidUser()
    {
        bool returnValue = LogonUser(_userName, _domain, _passWord,
                LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
                ref tokenHandle);

        Console.WriteLine("LogonUser called.");

        if (false == returnValue)
        {
            int ret = Marshal.GetLastWin32Error();
            Console.WriteLine("LogonUser failed with error code : {0}", ret);
            return false;
        }

        Console.WriteLine("Did LogonUser Succeed? " + (returnValue ? "Yes" : "No"));
        Console.WriteLine("Value of Windows NT token: " + tokenHandle);

        // Check the identity.
        Console.WriteLine("Before impersonation: "
            + WindowsIdentity.GetCurrent().Name);
        // Use the token handle returned by LogonUser.
        WindowsIdentity newId = new WindowsIdentity(tokenHandle);
        wic = newId.Impersonate();

        // Check the identity.
        Console.WriteLine("After impersonation: "
            + WindowsIdentity.GetCurrent().Name);
        return true;
    }
    #region IDisposable Members
    public void Dispose()
    {
        if(wic!=null)
            wic.Undo();
        if (tokenHandle != IntPtr.Zero)
            CloseHandle(tokenHandle);

    }
    #endregion
}
Community
  • 1
  • 1
Ryan
  • 615
  • 8
  • 19
  • I'm used to use advapi32.dll as your proposal. But if there have any inherited permission, it always throw exception. – R.Kaka Feb 22 '13 at 06:20