1

I am trying to connect to a network location through my ASP.NET web application using WNetAddConnection2. Every thing works perfectly when I debug it using the ASP.NET Development Server provided along with VS2010. But when I deploy the code in a Windows Server 2008 R2 it does not place the file and throws a Win32 exception with error code 0X80004005. The application pool identity is network service and i am using a domain user account for imparsanation.

I have checked the connectivity and user name password validity using the command and it works pefectly from the server runas /netonly /user:DOMAIN\User "explorer.exe \Path"

Please also note my firewall is switched off and i dont have an antivirus running on the Windows Server 2008.

Any help is really appreciated.

I have used the sample code from How to provide user name and password when connecting to a network share

Community
  • 1
  • 1
Anil S
  • 133
  • 1
  • 9

2 Answers2

1

Starting with Windows Vista Windows Service behave security-wise differently and are not allowed any "desktop-related activity" (like mounting someting)...

ASP.NET runs under IIS which runs as a Service - what you try is not supported and can only be achieved by circumventing several issues with permissions etc.

Not sure what you are trying to achieve ?

Perhaps there is a different way to achieve that goal...

Yahia
  • 69,653
  • 9
  • 115
  • 144
  • My ASP.NET application generates a CSV file which I need to drop to a network path (with specific permissions). The alternative is to create this CSV locally and another program (windows servive or a scheduled task console app) to pick this file and drop to the network location. Thought this is the easiest way - based on articles people got this working ...http://corner-house.blogspot.com/2006/03/accessing-shared-folders-in-aspnet.html – Anil S Sep 02 '11 at 04:29
  • I understand that code but as I said: these things changed with Vista and if you don't want to mess seriously with the permissions you can't get it working this way... I would suggest to just generate the CSV, store it locally and whoever needs it can get via a HTTP URL / GET... this can be a scheduled job or whatever app that is really running outside a Service... – Yahia Sep 02 '11 at 04:45
  • I have moved the code to a console application with scheduled task configured. However I see a intermittant behaviour with WNetAddConnection2. Is their any other way to connect to a network share? The code worked for once but after that it fails with the same error. – Anil S Sep 02 '11 at 09:57
  • Any reason that drive is not just mapped once persistently via Windows Explorer and subsequently used by the console program ? – Yahia Sep 02 '11 at 10:59
  • yes it is but then (when the user logged off) your console application isn't running either, or ? and when the user loggs on again the mapping is back - at least when you make is persistent... – Yahia Sep 03 '11 at 06:49
1

The WNetUseConnection will work, we need to define all its structures properly. WNetUseConnection(IntPtr.Zero, nr, password, username, 0, null, null, null);

Anil S
  • 133
  • 1
  • 9