The ASP.NET applications threads don't run as the person logged in. They
usually run another another account that you have configured in your ASP.NET
machine.config file in the <processModel >
So you'll either have to change who is running the ASP.NET thread or
impersonate the current thread as the person who is logged in.
Here's some impersonation code:
using System;
using System.Security .Principal;
using System.Security .Permissions;
using System.Runtime. InteropServices ;
using System.Threadin g;
namespace Impersonate
{
/// <summary>
/// Summary description for ImpersonateUser .
/// </summary>
public class ImpersonateUser
{
[DllImport("adva pi32.dll", SetLastError=tr ue)]
private static extern bool LogonUser(strin g lpszUsername,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider ,
ref IntPtr phToken);
[DllImport("kern el32.dll", CharSet=CharSet .Auto)]
private static extern bool CloseHandle(Int Ptr handle);
// constants used by LogonUser() method
private const int LOGON32_LOGON_N ETWORK = 3;
private const int LOGON32_PROVIDE R_DEFAULT = 0;
private WindowsImperson ationContext wic = null;
private WindowsIdentity currentIdentity = null;
public ImpersonateUser (string login, string password, string domain)
{
// Get current Identity
currentIdentity = WindowsIdentity .GetCurrent();
// handle returned from the LogonUser() method
IntPtr handle = new IntPtr(0);
handle = IntPtr.Zero;
// try to login to the domain
bool logonUser = LogonUser(login , domain, password,
LOGON32_LOGON_N ETWORK, LOGON32_PROVIDE R_DEFAULT, ref handle);
// login unsuccessful
if(!logonUser)
{
// get the error
int lastWin32Error = Marshal.GetLast Win32Error();
throw new Exception("Impe rsonateUser failed<br>Win32 Error: " +
lastWin32Error) ;
}
// create a new WindowsIdentity , set the CurrentPrincipa l and Impersonate
the user
WindowsIdentity wi
= new WindowsIdentity (handle, "NTLM", WindowsAccountT ype.Normal, true);
Thread.CurrentP rincipal = new WindowsPrincipa l(wi);
wic = wi.Impersonate( );
// close the handle
CloseHandle(han dle);
}
public void Undo()
{
// Impersonate back to original identity
wic.Undo();
Thread.CurrentP rincipal = new WindowsPrincipa l(currentIdenti ty);
currentIdentity .Impersonate();
}
}
}
"Shailesh Gajare" wrote:
Hi All,
I have creating an ASP.Net application with two web servers. I am
uploading a file which is being uploaded on one of the server, I want to
copy the uploaded file on the other server at the same time.
It says Access Denied.
I am using File.Copy ( Source , Destination , true ) command in my code,
where
Source : the current physical location of the uploaded file
Destination : the physical location of the other server i.e.
\\IPAddress\Sha redFolder
True : To overwrite the file if it exists
I have given rights for everyone on SharedFolder, but still it says Access
denied.
Is it that I have to provide login and pwd in my code.
Is it that I have to NetworkCredenti al class in my code? if Yes how do I
do.
What is that I am missing?
How do i give rights of ASPNet user of one machine to shared folder of other
machine.
Thanks for your help,
Shailesh Gajare