A small code example would be appreciated. I have tried using
this....(which
I found on this forum)
[DllImport("advapi32.dll")]
public static extern int LogonUser(String lpszUsername, String
lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, out
IntPtr phToken);
[DllImport("advapi32.DLL")]
public static extern bool ImpersonateLoggedOnUser(IntPtr hToken); //
handle to token for logged-on user
[DllImport("advapi32.DLL")] public static extern bool RevertToSelf();
public static void Main(string[] args)
{
IntPtr admin_token;
string password = ConfigurationSettings.AppSettings["NetDriveAccountPwd"];
WindowsIdentity wid_current = WindowsIdentity.GetCurrent();
//WindowsIdentity wid_admin = null;
//WindowsImpersonationContext wic = null;
try
{
Console.WriteLine("Copying file...");
if (LogonUser("userName", "192..168.1.1", password, 9, 0, out
admin_token) != 0)
{
ImpersonateLoggedOnUser(admin_token);
Console.WriteLine(WindowsIdentity.GetCurrent().Name);
//Console.WriteLine(admin_token.ToString());
//wid_admin = new WindowsIdentity(admin_token);
//wic = wid_admin.Impersonate();
System.IO.File.Copy("C:\\emailOutput.txt",
"\\\\192.168.1.1\\FolderName", true);
Console.WriteLine("Copy succeeded");
}
else Console.WriteLine("Copy Failed");
}
catch (System.Exception se)
{
int ret = Marshal.GetLastWin32Error();
Console.WriteLine(ret.ToString(), "Error code: " + ret.ToString());
Console.WriteLine(se.Message);
}
finally
{
RevertToSelf();
//if (wic != null) wic.Undo();
}
//Console.ReadLine();
}
--
But I kept on getting error "Access to the path
\\\\192.168.1.1\\FolderName
is denied"
Any help would be appreciated.
Ruepen
Ruepen said:
I need to write to a file on a shared drive on another server using user
credentials.
I can't have this permanently mapped either due to application server
restrictions that my app is running on.
Can this be done using C# classes only?