473,769 Members | 2,100 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Filecopy to network share

Hello,

I have a question. We have a webserver in a domain, DomainA, and a webserver
in a DMZ with local users and groups only.

I'm trying to copy a file from the DomainA webserver to the DMZ webserver.
Het firewall is configured to allow traffic via NetBIOS by ip-address. File
copy takes place in a .NET assembly.

Problem is described as follows: when copying I get an error 'access denied'
which is obvious. When connecting from Explorer (drive mapping) I can type
the IP\user and password, for example NLIIS405\copyus er password copyuser.
The mapping is created fine.

Trying to copy to \\NLIIS405\shar e it says access denied. I suspect I have
to do something using Windows Identity.

Could someone post me a sample in the right direction?

Regards,

Michel Smit
--
Michel Smit
Atos Origin Nederland BV
Jul 24 '06 #1
3 5710
Hello Michel,

Welcome to the MSDN newsgroup.

From your description, you're developing an .NET application which will
programmaticall y access a network share folder and copy some files into it.
Since the share folder is protected, you're encountering problems access it
in code, correct?

Based on my experience, according to your scenario, you have the following
two difficulties need to overcome:

1. Let your application(cur rent thread) running under a specific security
identity other than the default logon user (for winform or console
application).

2. Generate an identity/account on your webserver(where the code runs)
which can be used as our application's security identity, and this identity
should be authenticatable on the remote network share's machine.

For #1, we can use the .net platform invoke to call win32 "LogonUser" api
and impersonate our application code to run under the specific logon user
identity. The following kb article demonstrate how to use managed code to
perform impersonate(it applies to both desktop and asp.net application):

#How to implement impersonation in an ASP.NET application
http://support.microsoft.com/kb/306158/en-us
For #2, since the remote share is on a DMZ server (which has only local
users and groups), we can not domain account to access it, however, the
logonuser API can only access an account(credent ial) on local machine(for
your scenario it's the domainA webserver) or domain. To resolve this, you
need to create two duplicated account which have the same username and
password on both machines( the domainA webserver and the DMZ webserver).
Thus, on our domainA webserver, we can impersonate our application to run
under the "localmachi ne/duplicatedUser" account, and this account's Network
Credential can be used to access the remote DMZ server(and its share
folders). Also, you need to grant the permission for this duplicated
account on the DMZ server so as to manipulate the share folder.

I've paste a simple test console application's complete code at the bottom
of this message demonstrating the impersonate code(I've also include the
code file in this message and you can get it if you're using OE reader to
access the newsgroup).

Please feel free to let me know if you have anything unclear or any other
questions on this.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

=============== =============== =============== =====

Get notification to my posts through email? Please refer to

http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial

response from the community or a Microsoft Support Engineer within 1
business day is

acceptable. Please note that each follow up response may take approximately
2 business days

as the support professional working with you may need further investigation
to reach the

most efficient resolution. The offering is not appropriate for situations
that require

urgent, real-time or phone-based interactions or complex project analysis
and dump analysis

issues. Issues of this nature are best handled working with a dedicated
Microsoft Support

Engineer by contacting Microsoft Customer Support Services (CSS) at

http://msdn.microsoft.com/subscripti...t/default.aspx.

=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

===========main program file=========== =============== =
using System;
using System.Collecti ons.Generic;
using System.Text;
using System.Security .Principal;

namespace ImpersonateCons ole
{
class Program
{
static void Main(string[] args)
{
Console.WriteLi ne("Before Impersonate, User: {0}",
WindowsIdentity .GetCurrent().N ame);

if (ImpersonateHel per.Impersonate ValidUser("acco untname",
"localmachi ne or domain name", "Password") )
{
try
{
Console.WriteLi ne("After Impersonate, User: {0}",
WindowsIdentity .GetCurrent().N ame);

//add your remote file access code here

}
finally
{
ImpersonateHelp er.UndoImperson ation();
}
}
else
{
Console.WriteLi ne("Impersonat e failed......... .");
}


}
}
}
==========helpe r class code=========== ===

using System;
using System.Collecti ons.Generic;
using System.Text;
using System.Security ;
using System.Security .Principal;
using System.Runtime. InteropServices ;

namespace ImpersonateCons ole
{
public class ImpersonateHelp er
{
private static WindowsImperson ationContext impersonationCo ntext;

public const int LOGON32_LOGON_I NTERACTIVE = 2;
public const int LOGON32_PROVIDE R_DEFAULT = 0;

[DllImport("adva pi32.dll")]
public static extern int LogonUserA(Stri ng lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider ,
ref IntPtr phToken);
[DllImport("adva pi32.dll", CharSet = CharSet.Auto, SetLastError =
true)]
public static extern int DuplicateToken( IntPtr hToken,
int impersonationLe vel,
ref IntPtr hNewToken);

[DllImport("adva pi32.dll", CharSet = CharSet.Auto, SetLastError =
true)]
public static extern bool RevertToSelf();

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


public static bool ImpersonateVali dUser(String userName, String
domain, String password)
{
WindowsIdentity tempWindowsIden tity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;

if (RevertToSelf() )
{
if (LogonUserA(use rName, domain, password,
LOGON32_LOGON_I NTERACTIVE,
LOGON32_PROVIDE R_DEFAULT, ref token) != 0)
{
if (DuplicateToken (token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIden tity = new
WindowsIdentity (tokenDuplicate );
impersonationCo ntext =
tempWindowsIden tity.Impersonat e();
if (impersonationC ontext != null)
{
CloseHandle(tok en);
CloseHandle(tok enDuplicate);
return true;
}
}
}
}
if (token != IntPtr.Zero)
CloseHandle(tok en);
if (tokenDuplicate != IntPtr.Zero)
CloseHandle(tok enDuplicate);
return false;
}

public static void UndoImpersonati on()
{
impersonationCo ntext.Undo();
}

}
}

Jul 25 '06 #2
I'll give it a shot! Thanks!
--
Michel Smit
Atos Origin Nederland BV
"Steven Cheng[MSFT]" wrote:
Hello Michel,

Welcome to the MSDN newsgroup.

From your description, you're developing an .NET application which will
programmaticall y access a network share folder and copy some files into it.
Since the share folder is protected, you're encountering problems access it
in code, correct?

Based on my experience, according to your scenario, you have the following
two difficulties need to overcome:

1. Let your application(cur rent thread) running under a specific security
identity other than the default logon user (for winform or console
application).

2. Generate an identity/account on your webserver(where the code runs)
which can be used as our application's security identity, and this identity
should be authenticatable on the remote network share's machine.

For #1, we can use the .net platform invoke to call win32 "LogonUser" api
and impersonate our application code to run under the specific logon user
identity. The following kb article demonstrate how to use managed code to
perform impersonate(it applies to both desktop and asp.net application):

#How to implement impersonation in an ASP.NET application
http://support.microsoft.com/kb/306158/en-us
For #2, since the remote share is on a DMZ server (which has only local
users and groups), we can not domain account to access it, however, the
logonuser API can only access an account(credent ial) on local machine(for
your scenario it's the domainA webserver) or domain. To resolve this, you
need to create two duplicated account which have the same username and
password on both machines( the domainA webserver and the DMZ webserver).
Thus, on our domainA webserver, we can impersonate our application to run
under the "localmachi ne/duplicatedUser" account, and this account's Network
Credential can be used to access the remote DMZ server(and its share
folders). Also, you need to grant the permission for this duplicated
account on the DMZ server so as to manipulate the share folder.

I've paste a simple test console application's complete code at the bottom
of this message demonstrating the impersonate code(I've also include the
code file in this message and you can get it if you're using OE reader to
access the newsgroup).

Please feel free to let me know if you have anything unclear or any other
questions on this.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

=============== =============== =============== =====

Get notification to my posts through email? Please refer to

http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial

response from the community or a Microsoft Support Engineer within 1
business day is

acceptable. Please note that each follow up response may take approximately
2 business days

as the support professional working with you may need further investigation
to reach the

most efficient resolution. The offering is not appropriate for situations
that require

urgent, real-time or phone-based interactions or complex project analysis
and dump analysis

issues. Issues of this nature are best handled working with a dedicated
Microsoft Support

Engineer by contacting Microsoft Customer Support Services (CSS) at

http://msdn.microsoft.com/subscripti...t/default.aspx.

=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

===========main program file=========== =============== =
using System;
using System.Collecti ons.Generic;
using System.Text;
using System.Security .Principal;

namespace ImpersonateCons ole
{
class Program
{
static void Main(string[] args)
{
Console.WriteLi ne("Before Impersonate, User: {0}",
WindowsIdentity .GetCurrent().N ame);

if (ImpersonateHel per.Impersonate ValidUser("acco untname",
"localmachi ne or domain name", "Password") )
{
try
{
Console.WriteLi ne("After Impersonate, User: {0}",
WindowsIdentity .GetCurrent().N ame);

//add your remote file access code here

}
finally
{
ImpersonateHelp er.UndoImperson ation();
}
}
else
{
Console.WriteLi ne("Impersonat e failed......... .");
}


}
}
}
==========helpe r class code=========== ===

using System;
using System.Collecti ons.Generic;
using System.Text;
using System.Security ;
using System.Security .Principal;
using System.Runtime. InteropServices ;

namespace ImpersonateCons ole
{
public class ImpersonateHelp er
{
private static WindowsImperson ationContext impersonationCo ntext;

public const int LOGON32_LOGON_I NTERACTIVE = 2;
public const int LOGON32_PROVIDE R_DEFAULT = 0;

[DllImport("adva pi32.dll")]
public static extern int LogonUserA(Stri ng lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider ,
ref IntPtr phToken);
[DllImport("adva pi32.dll", CharSet = CharSet.Auto, SetLastError =
true)]
public static extern int DuplicateToken( IntPtr hToken,
int impersonationLe vel,
ref IntPtr hNewToken);

[DllImport("adva pi32.dll", CharSet = CharSet.Auto, SetLastError =
true)]
public static extern bool RevertToSelf();

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


public static bool ImpersonateVali dUser(String userName, String
domain, String password)
{
WindowsIdentity tempWindowsIden tity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;

if (RevertToSelf() )
{
if (LogonUserA(use rName, domain, password,
LOGON32_LOGON_I NTERACTIVE,
LOGON32_PROVIDE R_DEFAULT, ref token) != 0)
{
if (DuplicateToken (token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIden tity = new
WindowsIdentity (tokenDuplicate );
impersonationCo ntext =
tempWindowsIden tity.Impersonat e();
if (impersonationC ontext != null)
{
CloseHandle(tok en);
CloseHandle(tok enDuplicate);
return true;
}
}
}
}
if (token != IntPtr.Zero)
CloseHandle(tok en);
if (tokenDuplicate != IntPtr.Zero)
CloseHandle(tok enDuplicate);
return false;
}

public static void UndoImpersonati on()
{
impersonationCo ntext.Undo();
}

}
}


Jul 25 '06 #3
Thanks for your prompt response Michel,

Please feel free to let me know if you get any progress or meet any further
problem on this.

Good luck!

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 25 '06 #4

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

Similar topics

6
5874
by: deko | last post by:
In a multi-user environment, I have a table that stores hyperlinks to documents that are stored on the machine that hosts the mdb database. The table entry looks like this: ProductDescription.htm#file:\\DBHOST\C$\Documents and Settings\Administrator\My Documents\Products\Documents\ProductDescription.htm Having the hyperlink in this format allows the document to be opened by anyone on the local area network.
1
6571
by: brian.oneil2 | last post by:
Is there a way to install this onto a network file share and allow a team to access it? I would say share a CD from a networked CD drive, but there are multiple CD's that would have to be inserted. TIA, Brian
10
6339
by: BLiTZWiNG | last post by:
When I try the following: System.IO.File.Copy("C:\\test_read\\test.txt", "\\\\192.168.0.5\\test_write\\test.txt", false) I get an UnauthorizedAccessException. I cannot however, seem to find out how to authorize the file copy (ie. process the user/pass).
8
11853
by: Lam | last post by:
HI anyone knows how can I open a mapped network file in C#? I try string file = @"T:\file.txt"; it shows me the error: "Could not find a part of the path" but if I copy the file to my C dirve, and use @"C:\file.txt"; it worked Thanks a lot
3
5057
by: musosdev | last post by:
Hi guys Okay, I've setup my projects to open and compile fine in VS2005 using FPSE and remote web, but it's *really* slow. So I thought I'd have a go at doing it the normal way, by loading from the network share. It loads in VS2005 fine, and I can edit and save code changes etc, but when I try and Build the solution, I get the following error... An error occured loading a configuration file: Failed to start monitoring
4
4673
by: Jeremy S. | last post by:
We're in the process of writing a new Windows Forms app and the desktop support folks want for it to be run from a network share. I know it's possible (i.e., just have the framework on the clients and a desktop shortcut to the exe out on the network)... but is it really a good idea? What are some arguments for and against running a .NET Windows Forms client from a network share? Here is my initial list... I'd appreciate any additions,...
6
2971
by: tendim | last post by:
G'day group. Currently our organization us using VB6 based applications, and I am trying to push forward and migrate some of the smaller things to VB.NET, eventually migrating all applications from VB6 and other legacy languages/systems (Pure VBScript, DataEase, etc.) over to .NET. Currently, *all* user data is stored on network shares. When a user logs in to a workstation, their home drive is mounted from one share, all of their...
5
6686
by: lmttag | last post by:
ASP.NET 2.0 (C#) application Intranet application (not on the Internet) Using Windows authentication and impersonation Windows Server 2003 (IIS6) Server is a member server on a domain Logged into server as a domain user that is in the local Administrators group on the server Workstation is on the same domain Logged into the workstation as a domain user, which is also in the local Administrators group on the server and workstation
7
6128
by: bhughes2187 | last post by:
In my app I am creating, there is a procedure that does a file copy from the local drive to a network share. The issue I am having, is even though I have the share mapped, and I can browse to the share with no problems, when ever the procedure is run, I get a Permission Denied error. When I get this error, I click on Debug and find the line causing the error which is my file copy line. If I click continue, the code sequence finishes and the...
0
9422
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10038
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
9857
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
8867
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
7404
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
6662
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();...
1
3952
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3558
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2812
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.