473,503 Members | 3,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ASP.Net page Accessing Shared Resource

Hello,

We are going to have a web application that will be load balanced.
Uploading documents into the same server is not an option. So we need
to create a shared folder in a different machine and the 2 web servers
will be accessing the shared folder. Problem is, how can we access the
shared folder without setting the permissions accesible to anyone? We
read some articles about impersonation (impersonate the aspnet process
to an account who has only access to the shared folder) and right now
we are still struggling to make it work. Is there any other way to
access a shared folder from ASP.net page where the webserver and the
shared folder reside in the different machine?

any help is greatly appreciated.

Thanks,
Baldwin

Jan 18 '06 #1
2 1722
KJ
One thing we have done is to create identical virtual directories on
both web servers pointing to the same share. When setting up the
v-dirs, open the properties window, click the "connect as" button, and
enter the appropriate credentials. This make take some trial and error
to get working correctly. One thing also often necessary (when not
impersonating) is to give the ASPNET local server user (for both
servers) read permissions on the shared directory.

Another option is to try the code below which I have used to
successfully do impersonation under Windows auth (.net 1.1):

/// <summary>
/// This is used to programmatically
/// change the user principal on-demand.
///
/// It relies on the old Win32 APIs.
///
/// (This is made obsolete in .NET 2.0)
/// </summary>
public class Impersonation
{
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;

WindowsImpersonationContext impersonationContext;

[DllImport("advapi32.dll")]
public static extern int LogonUserA(String lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);

[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern int DuplicateToken(IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken);

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

[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern bool CloseHandle(IntPtr handle);

public bool ImpersonateValidWindowsUser(String userName, String
domain, String password)
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;

if(RevertToSelf())
{
if(LogonUserA(userName, domain, password,
LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if(DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null)
{
CloseHandle(token);
CloseHandle(tokenDuplicate);
return true;
}
}
}
}
if(token!= IntPtr.Zero)
CloseHandle(token);
if(tokenDuplicate!=IntPtr.Zero)
CloseHandle(tokenDuplicate);
return false;
}

public void WindowsAuth_Impersonate()
{
impersonationContext =
((System.Security.Principal.WindowsIdentity)HttpCo ntext.Current.User.Identity).Impersonate();
}

public void WindowsAuth_StopImpersonating()
{
impersonationContext.Undo();
}
}

Jan 18 '06 #2
thanks for the code. I will try this.

Jan 19 '06 #3

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

Similar topics

1
4525
by: Z0gS | last post by:
I got this problem for the web application I try to access files on a remote server. string dirs = Directory.GetDirectories(@"E:\vehicles") E drive is a map to a network drive. I get the...
9
5381
by: Bob Day | last post by:
VS 2003, vb.net , sql msde... I have an application with multiple threads running. Its a telephony application where each thread represents a telephone line. For code that would be the same...
3
2783
by: Grandpa Pete | last post by:
How can I share resources across all users of a web service for read/write access. *Situation* *one*: I want to have an in memory counter that all users of the webservice could access. Call...
8
12994
by: Ben Fidge | last post by:
Hi I have a small WinForms app that needs to copy files from a shared drive on a network. If I connect to the mapped drive using Explorer, a password dialog pops-up and I have to provide...
24
2957
by: Randall Arnold | last post by:
I know how to use MSHTML to grab the innerhtml from an html web page, but what if I want to look at elements of a rendered aspx page? Is there any way to do this using the MSHTML DOM object? ...
3
1969
by: C-Services Holland b.v. | last post by:
Hi all, I've created a custom button which I can stick into my toolbox. This all works well and I see my button in the designer. This button uses customised graphics and I want to stick it in...
2
1796
by: Rob Meade | last post by:
Hi all, I have an application which enables a user to register and select a series of resources to go with their profile. At the moment I have an issue on the section where they will select...
4
2417
by: accyboy1981 | last post by:
Hi, I'm looking to create a simple application that will copy files from a folder on one PC on the network to another PC on the network but I am having a bit of difficulty doing this. The...
3
2318
by: pantherSA | last post by:
Hi All, I have moved data from an old server (Win 2000) to a new server Win 2003 R2 Enterprise Edition. My local users can access the shared folder no hassle. My problem is my branch offices...
0
7093
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...
0
7291
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7357
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...
1
7012
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
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...
0
5598
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,...
0
4690
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...
0
3180
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...
0
1522
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 ...

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.