I have an app that needs to connect to a share on a NAS device and do some disk IO (Reading and writing out PDF documents)
I made a virtual directory in IIS that is working correctly (can access the files, etc).
I can not use the web.config
<identity impersonate="true" userName="user" password="password"/>
for impersonation, as most of the application needs to run as the default ASPNET user.
I made a test site using the above impersonation and it works fine (clears auth and can read/write files fine)
I went back and tried to implement a code based solution for an on demand impersonation and end up with the error:
Access to the path "\\NasDevice\Share\Doc_I_A
m_Saving.p
df" is denied.
I am not exactly sure why, as I am outputting the current login, and it looks correct, its switching from NETWORK SERVICE to the impersonated account and back.
Here is the code snippit related to the task:
IntPtr lnToken;
int TResult =LogonUser("Account",".","
Password",
LOGON32_LO
GON_NETWOR
K,LOGON32_
PROVIDER_D
EFAULT,out
lnToken);
if ( TResult > 0 )
{
ImpersonateLoggedOnUser(ln
Token);
StringBuilder sb = new StringBuilder(80,80);
uint Size = 79;
Response.Write(Page.User.I
dentity + " - " + System.Security.Principal.
WindowsIde
ntity.GetC
urrent() + " - " + System.Threading.Thread.Cu
rrentPrinc
ipal.Ident
ity);
Response.Write( Environment.UserName + " - " + this.User.Identity.Name + "<hr>");
string fn = System.IO.Path.GetFileName
(File1.Pos
tedFile.Fi
leName);
string SaveLocation = Server.MapPath("/PDF") + "\\" + fn;
try
{
File1.PostedFile.SaveAs(Sa
veLocation
);
Response.Write("The file has been uploaded." + SaveLocation);
}
catch ( Exception ex )
{
Response.Write("Error: " + ex.Message);
}
RevertToSelf();
Response.Write("<hr>" + Environment.UserName); CloseHandle(lnToken);
}
else { Response.Write("Not logged on: " + Environment.UserName); }
Start Free Trial