Credentials for network shares

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • GCeaser@aol.com

    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

  • Willy Denoyette [MVP]

    #2
    Re: Credentials for network shares


    <GCeaser@aol.co m> wrote in message
    news:1107192680 .691317.171590@ c13g2000cwb.goo glegroups.com.. .[color=blue]
    > 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[/color]

    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.


    Comment

    Working...