Credentials for network shares

G

GCeaser

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
 
W

Willy Denoyette [MVP]

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()).Message);
}
}
finally
{
Marshal.FreeHGlobal(use2.ui2_password);
Marshal.FreeHGlobal(pBuf);
}
}

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

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top