Thursday, January 20, 2005

OK, As promised in the previous part, I will give you the solution we used in our project to upload files.

Because, there's also a MS Access client, we have decide to upload our files to a network drive.  The MS Access programmers told us that they couldn't handle the large fields.  Now we had another problem.  Standard we don't have any authorization to write files on a network drive.  We could write only files on our own server.  So looking for a solution for this, I came accross Impersonation.  It seems that we can take over the identity of another user during the run of our upload page.  You can find all the info here.

Now we can open the upload page, and take the identity of a Functional Account, upload our file to the shared dir on the network, close the page and taken back the ASP worker identity.

A bit of code :

WindowsPrincipal p = (WindowsPrincipal)HttpContext.Current.User;
WindowsIdentity id = (WindowsIdentity)p.Identity;

Response.Output.Write("<h2>Process running as {0}</h2>", WindowsIdentity.GetCurrent().Name);

// impersonate temporarily
WindowsImpersonationContext wic = id.Impersonate();
try {
  // do some work while impersonating the client
  Response.Output.Write("<h2>Now impersonating {0}</h2>",WindowsIdentity.GetCurrent().Name);
}
finally {
  // restore our old security context
    wic.Undo();
}
Response.Output.Write("<h2>Once again running as {0}</h2>",WindowsIdentity.GetCurrent().Name);

1/20/2005 10:06:51 AM (Romance Standard Time, UTC+01:00)  #     | 
Tracked by:
"re:Dev " (液位开关) [Trackback]