473,543 Members | 2,065 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 16023

<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
7385
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...
6
2741
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
14445
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...
5
10163
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
2949
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. ...
0
4419
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...
5
6673
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...
2
2654
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
8502
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...
0
7355
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...
1
7356
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...
0
7697
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...
1
5285
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...
0
4899
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...
0
3394
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...
0
3394
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1830
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
1
979
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.