473,657 Members | 2,592 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

File.Copy : Access Denied

Hi All,
I have creating an ASP.Net application with two web servers. I am
uploading a file which is being uploaded on one of the server, I want to
copy the uploaded file on the other server at the same time.

It says Access Denied.

I am using File.Copy ( Source , Destination , true ) command in my code,
where
Source : the current physical location of the uploaded file
Destination : the physical location of the other server i.e.
\\IPAddress\Sha redFolder
True : To overwrite the file if it exists

I have given rights for everyone on SharedFolder, but still it says Access
denied.

Is it that I have to provide login and pwd in my code.
Is it that I have to NetworkCredenti al class in my code? if Yes how do I
do.

What is that I am missing?
How do i give rights of ASPNet user of one machine to shared folder of other
machine.

Thanks for your help,
Shailesh Gajare
Nov 19 '05 #1
2 3787
The ASP.NET applications threads don't run as the person logged in. They
usually run another another account that you have configured in your ASP.NET
machine.config file in the <processModel >

So you'll either have to change who is running the ASP.NET thread or
impersonate the current thread as the person who is logged in.

Here's some impersonation code:

using System;
using System.Security .Principal;
using System.Security .Permissions;
using System.Runtime. InteropServices ;
using System.Threadin g;

namespace Impersonate
{
/// <summary>
/// Summary description for ImpersonateUser .
/// </summary>
public class ImpersonateUser
{

[DllImport("adva pi32.dll", SetLastError=tr ue)]
private static extern bool LogonUser(strin g lpszUsername,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider ,
ref IntPtr phToken);

[DllImport("kern el32.dll", CharSet=CharSet .Auto)]
private static extern bool CloseHandle(Int Ptr handle);

// constants used by LogonUser() method
private const int LOGON32_LOGON_N ETWORK = 3;
private const int LOGON32_PROVIDE R_DEFAULT = 0;

private WindowsImperson ationContext wic = null;
private WindowsIdentity currentIdentity = null;

public ImpersonateUser (string login, string password, string domain)
{
// Get current Identity
currentIdentity = WindowsIdentity .GetCurrent();
// handle returned from the LogonUser() method
IntPtr handle = new IntPtr(0);
handle = IntPtr.Zero;
// try to login to the domain
bool logonUser = LogonUser(login , domain, password,
LOGON32_LOGON_N ETWORK, LOGON32_PROVIDE R_DEFAULT, ref handle);
// login unsuccessful
if(!logonUser)
{
// get the error
int lastWin32Error = Marshal.GetLast Win32Error();
throw new Exception("Impe rsonateUser failed<br>Win32 Error: " +
lastWin32Error) ;
}
// create a new WindowsIdentity , set the CurrentPrincipa l and Impersonate
the user
WindowsIdentity wi
= new WindowsIdentity (handle, "NTLM", WindowsAccountT ype.Normal, true);
Thread.CurrentP rincipal = new WindowsPrincipa l(wi);
wic = wi.Impersonate( );
// close the handle
CloseHandle(han dle);
}

public void Undo()
{
// Impersonate back to original identity
wic.Undo();
Thread.CurrentP rincipal = new WindowsPrincipa l(currentIdenti ty);
currentIdentity .Impersonate();
}

}

}

"Shailesh Gajare" wrote:
Hi All,
I have creating an ASP.Net application with two web servers. I am
uploading a file which is being uploaded on one of the server, I want to
copy the uploaded file on the other server at the same time.

It says Access Denied.

I am using File.Copy ( Source , Destination , true ) command in my code,
where
Source : the current physical location of the uploaded file
Destination : the physical location of the other server i.e.
\\IPAddress\Sha redFolder
True : To overwrite the file if it exists

I have given rights for everyone on SharedFolder, but still it says Access
denied.

Is it that I have to provide login and pwd in my code.
Is it that I have to NetworkCredenti al class in my code? if Yes how do I
do.

What is that I am missing?
How do i give rights of ASPNet user of one machine to shared folder of other
machine.

Thanks for your help,
Shailesh Gajare

Nov 19 '05 #2
by default, the asp.net account is a local account and cannot access network
shares. change the account to a domain account either up setting the identiy
in the app pool (2003) or in the web config:

<identity impersonate="tr ue" userName="domai n\account"
password="passw ord" />

note: to access a network share, you cannot impersonate the autheication
account unless you are using basic or kerberos (with delegation turned on)
authenication.

-- bruce (sqlwork.com)
"Shailesh Gajare" <sh************ *@ideaPool.com> wrote in message
news:e5******** ******@tk2msftn gp13.phx.gbl...
Hi All,
I have creating an ASP.Net application with two web servers. I am
uploading a file which is being uploaded on one of the server, I want to
copy the uploaded file on the other server at the same time.

It says Access Denied.

I am using File.Copy ( Source , Destination , true ) command in my code,
where
Source : the current physical location of the uploaded file
Destination : the physical location of the other server i.e.
\\IPAddress\Sha redFolder
True : To overwrite the file if it exists

I have given rights for everyone on SharedFolder, but still it says Access
denied.

Is it that I have to provide login and pwd in my code.
Is it that I have to NetworkCredenti al class in my code? if Yes how do I
do.

What is that I am missing?
How do i give rights of ASPNet user of one machine to shared folder of
other machine.

Thanks for your help,
Shailesh Gajare

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
15985
by: Divya | last post by:
Hello, I am trying to copy a file from one domain to another. I have the username and password of the destination domain. I tried a few approaches - 1. Using the Impersonator Class - http://www.codeproject.com/useritems/ZetaImpersonator.asp 2. Using WebRequest object to write the file to the destination domain - http://www.dotnet247.com/247reference/msgs/42/211482.aspx
2
4241
by: Narshe | last post by:
I have a custom action for my msi installer where you select a directory, and some files get copied to that directory. I'm using System.IO.File.Copy() to copy them. If the files are already there, they are supposed to get overwritten, but I'm getting an access denied error. The files that are currently there have RA permissions. If i remove the read-only permission, the installer works fine. How can I get my msi installer to overwrite...
2
3760
by: Stephen Witter | last post by:
I had previously posted this on the security ng, but haven't had a hit so I was wondering if someone here would be willing to take a stab. I am trying to copy a file to a network drive. I can do it on the web server but not from a client. Here is the code: Dim impersonationContext As System.Security.Principal.WindowsImpersonationContext Dim currentWindowsIdentity As System.Security.Principal.WindowsIdentity currentWindowsIdentity...
3
4046
by: Shailesh Gajare | last post by:
Hi All, I have creating an ASP.Net application with two web servers. I am uploading a file which is being uploaded on one of the server, I want to copy the uploaded file on the other server at the same time. It says Access Denied. I am using File.Copy ( Source , Destination , true ) command in my code, where Source : the current physical location of the uploaded file
5
38993
by: wo20051223 | last post by:
Deleting some files with C# fails with "Access to the path 'X' is denied". I have files copied from a CD that I burned (and not locked by a process) and a text file that I created in Windows Explorer. I can delete all of them through Windows Explorer. I can programmatically delete the text file but not the others. Permissions: - All files have the same ACL.
11
55940
by: trs-ggcsea | last post by:
Hi, I am reading a file from a UNC path in my Visual studio 2005 C# (.NET 2) program and getting an access denied exception. I am unsure if this access denied is due to the .NET security model, or because I need to implement impersonation of some sort. I was hoping someone could point me to an answer or the appropriate documentation, as I have as yet been unable to google up an answer. The account the program is running under is an...
4
4341
by: Thelma Lubkin | last post by:
I need to copy a file in a VBA module of an Access form, and I haven't been able to find out how to do it: just cp /Path1/file1 /Path2/file2 Please show me how to do this in VBA for Windows. thanks, --thelma
9
3926
by: pooba53 | last post by:
My VB .NET application (created in VS 2003) resides in c:\Program Files \Test\ and there's a data.mdb (Access) file within this directory. I have a feature in my program that allows the user to import a backup copy of this Access db if they need to. I'm using the FileCopy method to simply copy over the data.mdb file within the directory above. However, I get a "System.UnauthorizedAccessException: Access to the path: c:\Program...
1
15041
by: JordanRieger | last post by:
Here is a nasty issue that has been giving me grief for the last couple days. This requires good knowledge of IIS, MSXML, and Windows/NTFS permissions. We have an existing ASP (VBScript) app hosted on IIS 6.0 (W2K3). We need to restrict access to specific users within our company network. To reduce development effort I figured the easiest solution was to enable Integrated Windows Authentication. However once I enable IWA and disable...
0
8425
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8743
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8622
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5647
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.