Advertisement

05.14.2008 at 03:06PM PDT, ID: 23403345
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

6.4

C# file IO and impersonation Issues

Asked by FreedomIT in Microsoft IIS Web Server, Microsoft Visual C#.Net, .Net Editors & IDEs

Tags: , , ,

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_Am_Saving.pdf" 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_LOGON_NETWORK,LOGON32_PROVIDER_DEFAULT,out lnToken);

if ( TResult > 0 )
{
      ImpersonateLoggedOnUser(lnToken);
      StringBuilder sb = new StringBuilder(80,80);
      uint Size = 79;
      Response.Write(Page.User.Identity + " - " + System.Security.Principal.WindowsIdentity.GetCurrent() + " - " +       System.Threading.Thread.CurrentPrincipal.Identity);
      Response.Write( Environment.UserName + " - " + this.User.Identity.Name + "<hr>");
      string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);
      string SaveLocation = Server.MapPath("/PDF") + "\\" +  fn;
      
      try
      {
            File1.PostedFile.SaveAs(SaveLocation);
            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
 
Loading Advertisement...
 
[+][-]05.27.2008 at 09:46AM PDT, ID: 21653436

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft IIS Web Server, Microsoft Visual C#.Net, .Net Editors & IDEs
Tags: microsoft, C#, C#, IE
Sign Up Now!
Solution Provided By: FreedomIT
Participating Experts: 0
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628