Connect to a netword share via UNC name

Discussion in 'ASP .Net Security' started by Gaetano D'Aquila, Jan 19, 2004.

  1. Hi,
    There is anyone that have a piece of C# code that show how to use
    Directory.GetDirectories() over a UNC path?
    Example:
    I need to use Directory.GetDirectories(\\server\resource)
    I need to Impersonate "on-the-fly" an user before the call to
    Directory.GetDirectories and then restore the old account.
    Now I use this (at the end of e-mail) attached class to handle
    Impersonification, then I use

    CImpersonification i=new CImpersonification()
    if i.impersonate("username","password","domain")
    Directory.GetDirectories(\\\\server\\resource)

    But not work.
    Anyone can help me?
    Thank you.

    ===================== START HERE ========================================
    using System;
    using System.Runtime.InteropServices;
    using System.Security.Principal;
    using System.Security.Permissions;

    namespace test
    {
    public class CImpersonation
    {
    private WindowsIdentity newidentity;
    private WindowsImpersonationContext impcontext;

    // Declare the logon types as constants
    const long LOGON32_LOGON_INTERACTIVE = 2;
    const long LOGON32_LOGON_NETWORK = 3;

    // Declare the logon providers as constants
    const long LOGON32_PROVIDER_DEFAULT = 0;
    const long LOGON32_PROVIDER_WINNT50 = 3;
    const long LOGON32_PROVIDER_WINNT40 = 2;
    const long LOGON32_PROVIDER_WINNT35 = 1;

    [DllImport("advapi32.dll",EntryPoint = "LogonUser")]
    private static extern bool LogonUser(
    string lpszUsername,
    string lpszDomain,
    string lpszPassword,
    int dwLogonType,
    int dwLogonProvider,
    ref IntPtr phToken);

    public bool impersonate(
    string Username,
    string Password,
    string Domain)
    {
    // This is the token returned by the API call
    // Look forward to a future article covering
    // the uses of it
    IntPtr token = new IntPtr(0);
    token = IntPtr.Zero;

    // Call the API
    if (LogonUser(
    Username,
    Domain,
    Password,
    (int)LOGON32_LOGON_NETWORK,
    (int)LOGON32_PROVIDER_DEFAULT,
    ref token))
    {
    //' Since the API returned TRUE, return TRUE to the caller
    newidentity = WindowsIdentity.GetCurrent();
    impcontext = WindowsIdentity.Impersonate(token);
    return true;
    }
    else
    {
    //' Bad credentials, return FALSE
    return false ;
    }
    }

    public void revertimpersonation()
    {
    impcontext.Undo();
    //newidentity = Nothing;
    }
    }
    }


    ===================== STOP HERE ==========================================
    Gaetano D'Aquila, Jan 19, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Chung-Jen Chuang
    Replies:
    1
    Views:
    3,943
    Ken Schaefer
    Aug 23, 2003
  2. Gaetano D'Aquila

    Connect to a netword share via UNC name

    Gaetano D'Aquila, Jan 19, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    604
    Gaetano D'Aquila
    Jan 19, 2004
  3. Amol Shambharker
    Replies:
    1
    Views:
    423
    Cor Ligthert
    Apr 30, 2004
  4. =?Utf-8?B?cGpfc2VydmFkbWlu?=

    Running an asp.net application on a remote share (UNC) - parser er

    =?Utf-8?B?cGpfc2VydmFkbWlu?=, Jun 20, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,586
    =?Utf-8?B?cGpfc2VydmFkbWlu?=
    Jul 1, 2005
  5. Saraswati lakki
    Replies:
    0
    Views:
    1,287
    Saraswati lakki
    Jan 6, 2012
Loading...

Share This Page