Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3228052
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:38:48+00:00 2026-05-17T16:38:48+00:00

my application use a shared directory (Windows) on the network and i want to

  • 0

my application use a shared directory (Windows) on the network and i want to make that share available on network only to a given user and password.
Is there any way to pass authentication prior to any operations on this network resource? *I’m not using a domain.

Thank you so much!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:38:48+00:00Added an answer on May 17, 2026 at 4:38 pm

    I’m not totally sure how you would pass credentials for IO operations, but typically I use an impersonation class to impersonate or delegate user credentials for a specific block of code.

    E.g.:

    /// <summary>
    /// Provides a mechanism for impersonating a user.  This is intended to be disposable, and
    /// used in a using ( ) block.
    /// </summary>
    public class Impersonation : IDisposable
    {
        #region Externals
        [DllImport("advapi32.dll", SetLastError = true)]
        private static extern bool LogonUser(
            string lpszUsername,
            string lpszDomain,
            string lpszPassword,
            int dwLogonType,
            int dwLogonProvider,
            out IntPtr phToken);
    
        [DllImport("advapi32.dll", SetLastError = true)]
        private extern static bool DuplicateToken(IntPtr ExistingTokenHandle, int
           SECURITY_IMPERSONATION_LEVEL, out IntPtr DuplicateTokenHandle);
    
        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool CloseHandle(IntPtr hObject);
        #endregion
    
        #region Fields
        private IntPtr token;
        private IntPtr tokenDuplicate;
    
        private WindowsIdentity identity;
        private WindowsImpersonationContext context;
    
        private readonly string domain;
        private readonly string username;
        private readonly string password;
        private ImpersonationLevel level;
        #endregion
    
        #region Constructor
        /// <summary>
        /// Initialises a new instance of <see cref="Impersonation"/>.
        /// </summary>
        /// <param name="domain">The domain of the target user.</param>
        /// <param name="username">The target user to impersonate.</param>
        /// <param name="password">The target password of the user to impersonate.</param>
        public Impersonation(string domain, string username, string password)
        {
            this.domain = domain;
            this.username = username;
            this.password = password;
            this.level = ImpersonationLevel.Impersonation;
    
            Logon();
        }
    
        /// <summary>
        /// Initialises a new instance of <see cref="Impersonation"/>.
        /// </summary>
        /// <param name="domain">The domain of the target user.</param>
        /// <param name="username">The target user to impersonate.</param>
        /// <param name="password">The target password of the user to impersonate.</param>
        /// <param name="level">The security level of this impersonation.</param>
        public Impersonation(string domain, string username, string password, ImpersonationLevel level)
        {
            this.domain = domain;
            this.username = username;
            this.password = password;
            this.level = level;
    
            Logon();
        }
        #endregion
    
        #region Methods
        /// <summary>
        /// Reverts the impersonation.
        /// </summary>
        public void Dispose()
        {
            if (context != null)
                context.Undo();
    
            if (token != IntPtr.Zero)
                CloseHandle(token);
    
            if (tokenDuplicate != IntPtr.Zero)
                CloseHandle(tokenDuplicate);
        }
    
        /// <summary>
        /// Performs the logon.
        /// </summary>
        private void Logon()
        {
            if (LogonUser(username, domain, password, 2, 0, out token))
            {
                if (DuplicateToken(token, (int)level, out tokenDuplicate))
                {
                    identity = new WindowsIdentity(tokenDuplicate);
                    context = identity.Impersonate();
                }
                else
                {
                    throw new SecurityException("Unable to impersonate the user.");
                }
            }
            else
            {
                throw new SecurityException("The login details you have entered were incorrect.");
            }
        }
        #endregion
    }
    
    /// <summary>
    /// Defines the possible security levels for impersonation.
    /// </summary>
    public enum ImpersonationLevel
    {
        /// <summary>
        /// Anonymous access, the process is unable to identify the security context.
        /// </summary>
        Anonymous = 0,
        /// <summary>
        /// The process can identify the security context.
        /// </summary>
        Identification = 1,
        /// <summary>
        /// The security context can be used to access local resources.
        /// </summary>
        Impersonation = 2,
        /// <summary>
        /// The security context can be used to access remote resources.
        /// </summary>
        Delegation = 3
    }
    

    Now, it does involve a little P/Invoke, but the end result is:

    class Program
    {
        static void Main(string[] args)
        {
            using (var impersonation = new Impersonation("domain", "username", "password", ImpersonationLevel.Delegation))
            {
                // Do remote operations here.
            }
        }
    }
    

    For a given segment of code, you can impersonate the required user to perform your operations. If used in a using block, after that segment of code is executed, the impersonation is reverted and the handles closed.

    If you don’t use a using block, you need to ensure you call Dispose to clear everything up.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET application running on shared hosting and want to use Windows
A user installed our application to a server PC, shared the install directory as
I want to use the SquishIt nuget package inside and MVC3 application that is
Somebody said that when your PHP code and application use global variables then it
An application I use, Mozy Backup , adds its own drive to Windows Explorer
In my application I use only WebView for loading index.html where running all app
I have a Asp.Net MVC application that runs on a shared host under IIS7.
I'm working in an ASP.NET (VB) Web Application with Windows/Active Directory Authentication I am
I have a shared library written in C++ that I'd like to use with
I use boost::shared_ptr in my application in C++. The memory problem is really serious,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.