473,890 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Credentials for network shares

All,
I have a situation where a Server exists in a stand alone workgroup
on an Active Directory domain. The server can access domain resources
with the correct domain security information provided (for example I
can map a drive from the server to a server in the AD by connecting as
a different user and providing credentials that the AD can
authenticate.)

The challenge is that I want to access a domain network share
programmaticall y via C# from the workgroup server. Does anyone know
how I can programmaticall y map to the drive and provide the domain
credentials?

Thanks
George

Nov 16 '05 #1
1 16105

<GC*****@aol.co m> wrote in message
news:11******** **************@ c13g2000cwb.goo glegroups.com.. .
All,
I have a situation where a Server exists in a stand alone workgroup
on an Active Directory domain. The server can access domain resources
with the correct domain security information provided (for example I
can map a drive from the server to a server in the AD by connecting as
a different user and providing credentials that the AD can
authenticate.)

The challenge is that I want to access a domain network share
programmaticall y via C# from the workgroup server. Does anyone know
how I can programmaticall y map to the drive and provide the domain
credentials?

Thanks
George


You have multiple options to access a remote file share, here are a few:
1. Map the drive in a Logonscript.
2. User Process.Start to execute "net use \\xxxx\yyyy password /user:uuuu
...."
3. When running XP or higher create a logon session by calling LogonUser
with Logontype LOGON32_LOGON_N EW_CREDENTIALS. This returns a token with
network access privileges using the credentilas specified, but it keeps the
user token for local access checks.
4. Use Pinvoke to call Win32 API NetUseAdd(...) and USE_INFO_2 structure.

Herewith a sample:

[StructLayout(La youtKind.Sequen tial,CharSet=Ch arSet.Auto)]
struct _USE_INFO_2
{
internal string ui2_local;
internal string ui2_remote;
internal IntPtr ui2_password; // don't pass a string or StringBuilder
here!!
internal uint ui2_status;
internal uint ui2_asg_type;
internal uint ui2_refcount;
internal uint ui2_usecount;
internal string ui2_username;
internal string ui2_domainname;
}

[DllImport("neta pi32", SetLastError=tr ue),
SuppressUnmanag edCodeSecurityA ttribute]
static extern int NetUseAdd(
string UncServerName, // not used
int Level, // use info struct
IntPtr Buf, // Buffer
ref int ParmError
);

// Establish a use record
public static void UseRecord(strin g remotePath, string user, string
password, string domain)
{
int ret = 0;
int paramError = 0;
_USE_INFO_2 use2 = new _USE_INFO_2();
IntPtr pBuf = IntPtr.Zero;
use2.ui2_passwo rd = IntPtr.Zero;
pBuf = Marshal.AllocHG lobal(Marshal.S izeOf(use2));
use2.ui2_local = null;
use2.ui2_asg_ty pe = USE_WILDCARD;
use2.ui2_remote = remotePath;
use2.ui2_passwo rd = Marshal.StringT oHGlobalAuto(pa ssword);
use2.ui2_userna me = user;
use2.ui2_domain name = domain;
Marshal.Structu reToPtr(use2, pBuf, true);
ret = NetUseAdd(null, 2, pBuf, ref paramError);
if(ret != 0)
{
throw new Exception(new
Win32Exception( Marshal.GetLast Win32Error()).M essage);
}
}
finally
{
Marshal.FreeHGl obal(use2.ui2_p assword);
Marshal.FreeHGl obal(pBuf);
}
}

Usage:
UseRecord("\\\\ server\\share", "userName", "pwd", "domain");

Willy.
Nov 16 '05 #2

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

Similar topics

0
7410
by: Homer Blankenship | last post by:
Every day we get several errors like or similar to this one while a ..net app is trying to write files to a network share. The .net app breaks the files up into chunks and writes them to this share as it recieves the file over the Internet. Sometimes it fails with a Network path not found error like this one and other times it will fail with a No file to append to error after it has written several chunks of data and it's like it just...
6
2757
by: Default User | last post by:
Hi How can i look for network share folders and copy a text file to it? I've been looking into how i can do this in the documention but with no luck. I found a little on of the web but i dont understand it, it seems to import mpr.dll and make the use of c++ calls. Can this be just done by c#? If so how? Jamie C
4
14473
by: Heliotic | last post by:
Hi everyone, Currently I am working on an application that will perform a remote scan of a specified server using the following code: Type t = Type.GetTypeFromProgID( "Microsoft.Update.Session", "proact" ); UpdateSession UpdSess = (UpdateSession) Activator.CreateInstance(t); This code works quite well, as long as the user you are currently authenticated to also has administrative access on the target device. This
5
10184
by: Josh Rolfe | last post by:
I have a page in classic asp that accces a network drive, The code is as follows: <% dim fso dim objFolder set fso=server.createObject("Scripting.FileSystemObject") set objFolder=fso.GetFolder("f:\") for each objFile in objFolder.files response.write objFile.name & "<br>" next
6
2976
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...
0
4526
by: NoWhereMan | last post by:
Hello, I'm trying to get the list of shared folders of a network computer and add them to a combo box. I'm using the bellow code in VB .NET. Code ==== Private Sub EnumShares(ByVal Machine As String) 'Local Shares Try Dim scope As String = ("\\" & Machine & "\root\cimv2") Dim SharesClass As ManagementClass = New ManagementClass(scope, "Win32_Share", Nothing)
5
6696
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
2
2669
by: raibeart | last post by:
Is there a way in dot net to get to different shares using different logins that are not apparent to the user. The user would not normally have access to these shares on the network, but through the program, would, but would not realize it. Thanks in advance, Robert
2
8569
by: IronMike | last post by:
I'm writing a program that needs to write files to a network share. My problem is that the program users are working on PCs that are not connected to our domain and the program needs to save files to a network share on the domain. It's not feasible to change all of the user accounts on the PCs to match the user's domain credentials so I want to prompt the user for credentials (they all have domain accounts to access Citrix) and then use those...
0
10830
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...
1
10925
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10468
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
9641
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
8018
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
5855
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
6061
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4276
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3283
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.