473,394 Members | 2,031 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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
programmatically via C# from the workgroup server. Does anyone know
how I can programmatically map to the drive and provide the domain
credentials?

Thanks
George

Nov 16 '05 #1
1 16006

<GC*****@aol.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.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
programmatically via C# from the workgroup server. Does anyone know
how I can programmatically 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_NEW_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(LayoutKind.Sequential,CharSet=CharSet .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("netapi32", SetLastError=true),
SuppressUnmanagedCodeSecurityAttribute]
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(string 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_password = IntPtr.Zero;
pBuf = Marshal.AllocHGlobal(Marshal.SizeOf(use2));
use2.ui2_local = null;
use2.ui2_asg_type = USE_WILDCARD;
use2.ui2_remote = remotePath;
use2.ui2_password = Marshal.StringToHGlobalAuto(password);
use2.ui2_username = user;
use2.ui2_domainname = domain;
Marshal.StructureToPtr(use2, pBuf, true);
ret = NetUseAdd(null, 2, pBuf, ref paramError);
if(ret != 0)
{
throw new Exception(new
Win32Exception(Marshal.GetLastWin32Error()).Messag e);
}
}
finally
{
Marshal.FreeHGlobal(use2.ui2_password);
Marshal.FreeHGlobal(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
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...
6
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...
4
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(...
5
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...
6
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...
0
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...
5
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...
2
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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
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...

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.