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 9274201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:18:00+00:00 2026-06-18T16:18:00+00:00

When I tried to access network drive file from local its working fine but

  • 0

When I tried to access network drive file from local its working fine but when i deploy the code I am getting bellow error

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)

at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)

at System.Web.HttpResponse.WriteFile(String filename, Boolean readIntoMemory)

at System.Web.HttpResponse.WriteFile(String filename)

at Configs.gvConfigs_RowCommand(Object sender, GridViewCommandEventArgs e) in C:\Users\bpucha1103c\Desktop\CellBackHaul_Publish\Configs.aspx.cs:line 59
2013-02-05 13:31:21,412 [19] WARN System.Web.UI.Page [(null)] – Logging:System.IO.IOException: The account used is a computer account. Use your global user account or local user account to access this server.

how to do impersonation when accessing the file in network shared folder? Below is my code

GridViewRow rw = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
LinkButton lnkTxtFile = (LinkButton)rw.FindControl("lnkTxtFile");
string strFilename = lnkTxtFile.Text.Replace("/","\\");
System.IO.FileInfo targetFile = new System.IO.FileInfo(strFilename);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + targetFile.Name);
Response.ContentType = "application/octet-stream";
Response.WriteFile(targetFile.FullName);
//HttpContext.Current.ApplicationInstance.CompleteRequest();
Response.End();

here is my modified code

GridViewRow rw = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
LinkButton lnkTxtFile = (LinkButton)rw.FindControl("lnkTxtFile");
string strFilename = lnkTxtFile.Text.Replace("/", "\\");
System.IO.FileInfo targetFile = new System.IO.FileInfo(strFilename);
RunOperationAsUser(() =>
{
  //GridViewRow rw = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
  //LinkButton lnkTxtFile = (LinkButton)rw.FindControl("lnkTxtFile");
  //string strFilename = lnkTxtFile.Text.Replace("/", "\\");
  //System.IO.FileInfo targetFile = new System.IO.FileInfo(strFilename);
  Response.Clear();
  Response.AddHeader("Content-Disposition", "attachment; filename=" + targetFile.Name);
  Response.ContentType = "application/octet-stream";
  Response.WriteFile(targetFile.FullName);
  //HttpContext.Current.ApplicationInstance.CompleteRequest();
  Response.End();
}, "bpucha1103c", targetFile.DirectoryName , "White1234");
  • 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-18T16:18:01+00:00Added an answer on June 18, 2026 at 4:18 pm

    You can try to use the following code to impersonate as another user and run an action:

    [DllImport("advapi32.dll", SetLastError = true)]
    private static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public extern static bool DuplicateToken(IntPtr existingTokenHandle, int impersonationLevel, ref IntPtr duplicateTokenHandle);
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern bool CloseHandle(IntPtr handle);
    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool RevertToSelf();
    [DllImport("userenv.dll", SetLastError = true, CharSet = CharSet.Auto)]
    public static extern bool LoadUserProfile(IntPtr hToken, ref ProfileInfo lpProfileInfo);
    
    [DllImport("Userenv.dll", CallingConvention =
        CallingConvention.Winapi, SetLastError = true, CharSet = CharSet.Auto)]
    public static extern bool UnloadUserProfile
        (IntPtr hToken, IntPtr lpProfileInfo);
    
    [DllImport("ole32.dll")]
    public static extern int CoInitializeSecurity(IntPtr pVoid, int
        cAuthSvc, IntPtr asAuthSvc, IntPtr pReserved1, RpcAuthnLevel level,
        RpcImpLevel impers, IntPtr pAuthList, EoAuthnCap dwCapabilities, IntPtr
        pReserved3);
    
    [StructLayout(LayoutKind.Sequential)]
    public struct ProfileInfo
    {
        ///
        /// Specifies the size of the structure, in bytes.
        ///
        public int dwSize;
    
        ///
        /// This member can be one of the following flags: 
        /// PI_NOUI or PI_APPLYPOLICY
        ///
        public int dwFlags;
    
        ///
        /// Pointer to the name of the user.
        /// This member is used as the base name of the directory 
        /// in which to store a new profile.
        ///
        public string lpUserName;
    
        ///
        /// Pointer to the roaming user profile path.
        /// If the user does not have a roaming profile, this member can be NULL.
        ///
        public string lpProfilePath;
    
        ///
        /// Pointer to the default user profile path. This member can be NULL.
        ///
        public string lpDefaultPath;
    
        ///
        /// Pointer to the name of the validating domain controller, in NetBIOS format.
        /// If this member is NULL, the Windows NT 4.0-style policy will not be applied.
        ///
        public string lpServerName;
    
        ///
        /// Pointer to the path of the Windows NT 4.0-style policy file. 
        /// This member can be NULL.
        ///
        public string lpPolicyPath;
    
        ///
        /// Handle to the HKEY_CURRENT_USER registry key.
        ///
        public IntPtr hProfile;
    }
    
    public enum RpcAuthnLevel
    {
        Default = 0,
        None = 1,
        Connect = 2,
        Call = 3,
        Pkt = 4,
        PktIntegrity = 5,
        PktPrivacy = 6
    }
    
    public enum RpcImpLevel
    {
        Default = 0,
        Anonymous = 1,
        Identify = 2,
        Impersonate = 3,
        Delegate = 4
    }
    
    public enum EoAuthnCap
    {
        None = 0x00,
        MutualAuth = 0x01,
        StaticCloaking = 0x20,
        DynamicCloaking = 0x40,
        AnyAuthority = 0x80,
        MakeFullSIC = 0x100,
        Default = 0x800,
        SecureRefs = 0x02,
        AccessControl = 0x04,
        AppID = 0x08,
        Dynamic = 0x10,
        RequireFullSIC = 0x200,
        AutoImpersonate = 0x400,
        NoCustomMarshal = 0x2000,
        DisableAAA = 0x1000
    }
    
    const int LOGON32_PROVIDER_DEFAULT = 0;
    const int LOGON32_LOGON_NEW_CREDENTIALS = 9;
    const int SECURITY_IMPERSONATION_LEVEL = 2;
    
    public void RunOperationAsUser(Action operation, string userName, string domain, string password)
    {
        IntPtr token = IntPtr.Zero;
        IntPtr dupToken = IntPtr.Zero;
    
        //Impersonate the user
        if (LogonUser(userName, domain, password, LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_DEFAULT, ref token))
        {
            if (DuplicateToken(token, SECURITY_IMPERSONATION_LEVEL, ref dupToken))
            {
    
                WindowsIdentity newIdentity = new WindowsIdentity(dupToken);
                WindowsImpersonationContext impersonatedUser = newIdentity.Impersonate();
    
                int retCode = CoInitializeSecurity(IntPtr.Zero, -1, IntPtr.Zero, IntPtr.Zero,
                    RpcAuthnLevel.PktPrivacy, RpcImpLevel.Impersonate, IntPtr.Zero, EoAuthnCap.DynamicCloaking, IntPtr.Zero);
    
                if (impersonatedUser != null)
                {
                    var username = WindowsIdentity.GetCurrent(TokenAccessLevels.MaximumAllowed).Name;
                    var sid = WindowsIdentity.GetCurrent(TokenAccessLevels.MaximumAllowed).User.Value;
    
                    ProfileInfo profileInfo = new ProfileInfo();
                    profileInfo.dwSize = Marshal.SizeOf(profileInfo);
                    profileInfo.lpUserName = userName;
                    profileInfo.dwFlags = 1;
    
                    Boolean loadSuccess = LoadUserProfile(dupToken, ref profileInfo);
                }
    
                operation();
    
                impersonatedUser.Undo();
            }
        }
        if (token != IntPtr.Zero)
        {
            CloseHandle(token);
        }
        if (dupToken != IntPtr.Zero)
        {
            try
            {
                CloseHandle(token);
            }
            catch
            { }
        }
    }
    

    now you can do

    RunOperationAsUser(() => {
        GridViewRow rw = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
        LinkButton lnkTxtFile = (LinkButton)rw.FindControl("lnkTxtFile");
        string strFilename = lnkTxtFile.Text.Replace("/","\\");
        System.IO.FileInfo targetFile = new System.IO.FileInfo(strFilename);
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename=" + targetFile.Name);
        Response.ContentType = "application/octet-stream";
        Response.WriteFile(targetFile.FullName);
        //HttpContext.Current.ApplicationInstance.CompleteRequest();
        Response.End();
    }, userName, domain, password)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get following error when trying to access Xampp from a network I've tried
I am trying to access a page method from centralized module. I tried putting
We use MVC controllers that access System.File.IO in our application and they work fine
I cloned a Git repository from Windows using msysgit (v1.7.7) on a network drive.
I am trying to get a symlink working over a network drive. I have
I'm learning PhoneGap and I tried to use the Notification Local Plugin , but
I mapped the network drive W:\ directory.Load image file to load the path specified
I am trying to access C drive of a network PC. I am able
Is there any way to access a Windows hidden share from Cygwin? I've tried:
I tried to access my Gmail mailbox with InterImap Library. It shows my messages

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.