Impersonation in ASP.NET MVC

Viewed 27632

I have a MVC web application on an intranet and want to be able to create files on our FTP server to send to outside partners.

The code for impersonation uses the WindowsImpersonationContext.

System.Security.Principal.WindowsImpersonationContext impersonationContext;
impersonationContext = ((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();

StreamWriter sw = System.IO.File.CreateText("PathOnFTPServer");
sw.Write("data");

impersonationContext.Undo();

Here's what's happening and the reason for my question:

Pre Impersonation

User.Identity.Name: [my windows credentials]

System.Security.Principal.WindowsIdentity.GetCurrent().Name: NT AUTHORITY\NETWORK SERVICE

Post Impersonation

User.Identity: [my windows credentials]

GetCurrent.Name: [my windows credentials]

Impersonate Undo

User.Identity: [my windows credentials]

GetCurrent.Name: NT AUTHORITY\NETWORK SERVICE

So, before I impersonate, the current user is the System Account but after impersonation, it is using my windows domain account which has permission to create text files on the FTP server. The code works locally using the visual studio web server but not when I deploy it on IIS on our test server.

I'm getting an access denied error. What would be the reason for the error when the correct user is being impersonated?

1 Answers
Related