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

  • SEARCH
  • Home
  • 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 8080125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:19:22+00:00 2026-06-05T16:19:22+00:00

I have been working on a project in C# (.net4). Project pretty much allows

  • 0

I have been working on a project in C# (.net4). Project pretty much allows people to upload files from their local machine to a network share.

Network share is secured. It is only accessible by user called “proxy” which is created in active directory.

I did some research and I found this class which I used for impersonation.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Security.Principal;

namespace Datacom.CorporateSys.Utilities
{
    public class ImpersonateUser
    {
        [DllImport("advapi32.dll")]
        public static extern int LogonUserA(String lpszUserName,
            String lpszDomain,
            String lpszPassword,
            int dwLogonType,
            int dwLogonProvider,
            ref IntPtr phToken);
        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern int DuplicateToken(IntPtr hToken,
            int impersonationLevel,
            ref IntPtr hNewToken);

        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool RevertToSelf();

        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern bool CloseHandle(IntPtr handle);

        WindowsImpersonationContext impersonationContext;

        public const int LOGON32_LOGON_INTERACTIVE = 2;
        public const int LOGON32_PROVIDER_DEFAULT = 0;
        private string p;
        private string p_2;
        private string p_3;


        private String UserName
        {
            set;
            get;
        }

        private String Domain
        {
            set;
            get;
        }

        private String Password
        {
            set;
            get;
        }

        /// <summary>
        /// Impersonates the user.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="domain">The domain.</param>
        /// <param name="password">The password.</param>
        public ImpersonateUser(string userName, string domain, string password)
        {
            UserName = userName;
            Domain = domain;
            Password = password;
        }

        /// <summary>
        /// Impersonates the valid user.
        /// </summary>
        /// <returns></returns>
        public bool impersonateValidUser()
        {
            WindowsIdentity tempWindowsIdentity;
            IntPtr token = IntPtr.Zero;
            IntPtr tokenDuplicate = IntPtr.Zero;

            if (RevertToSelf())
            {
                if (LogonUserA(UserName, Domain, Password, LOGON32_LOGON_INTERACTIVE,
                    LOGON32_PROVIDER_DEFAULT, ref token) != 0)
                {
                    if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
                    {
                        tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                        impersonationContext = tempWindowsIdentity.Impersonate();
                        if (impersonationContext != null)
                        {
                            CloseHandle(token);
                            CloseHandle(tokenDuplicate);
                            return true;
                        }
                    }
                }
            }
            if (token != IntPtr.Zero)
                CloseHandle(token);
            if (tokenDuplicate != IntPtr.Zero)
                CloseHandle(tokenDuplicate);
            return false;
        }

        /// <summary>
        /// Undoes the impersonation.
        /// </summary>
        public void undoImpersonation()
        {
            impersonationContext.Undo();
        }
    }
}

Note: From memory I think I found this as an example on msdn.

This is how I try to move a file from local path to network

 if (imp.impersonateValidUser())
                            {
                                System.IO.File.Copy(local_file, server_file, true);
                                imp.undoImpersonation();
                            }
                            else
                            {
                                throw new Exception("Unable to impersonate for uploading file.");
                            }

And it works! I never have issues with impersonation – exception never gets thrown. It works fine and uploads files to the server. However, when I started testing a bit more I found that if proxy user is not logged in to the server (normally I open RDS login and quit – without logging out).

I get different exception – network path not found exception and it only occurs when I have just restarted the server and “proxy” is not logged in.

My first thought was that there is something wrong with impersonation class, however it impersonates fine when it does work (ie. files have ownership of proxy user). Then I thought that maybe “proxy” needs to be logged in so OS can use its permissions to actually access \server\uploads

I’m extremely lost now, not sure how to solve it.
Please note: I have no control over the server. Server is win2k8 with desktop experience installed (otherwise i cant access any network locations).

Thanks!

  • 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-06-05T16:19:24+00:00Added an answer on June 5, 2026 at 4:19 pm

    Grant the proxy account access right “Log on as a batch Job” and use `LOGON32_LOGON_BATCH instead of interactive logon.

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

Sidebar

Related Questions

I have been working on project, to make a website which read files(c/c++ text
So I have been working on a project for a client on their current
I have been working on a project on and off, but I haven't touched
Situation I have been working on a project lately where the UI development seems
So I have been working on this project for a short while now. I
Hey so I have been working on a project that I want to be
I have been working on an opencv project for iOS. I was given a
I have been working on a huge ETL project with 150+ tables and during
I have been working on a C# 4.0 WPF project and need to figure
I have been working on keeping things object oriented for my project. Currently, I'm

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.