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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:14:41+00:00 2026-06-16T00:14:41+00:00

I want to read files from a folder that exists on the network. When

  • 0

I want to read files from a folder that exists on the network.

When I try to access this folder manually (from run command giving a path like \\ABCServer\Documents ) it ask me for credentials (username and password). After giving the correct credentials I am able to access/read files.

When I try to read the same files from C# code in ASP.NET it gives me an error:

Login Failure: unkown username or bad password

How can I pass credentials via C# code during reading file?

Below is a part of the code that I am using:

Stream s = File.OpenRead(filePath);
byte[] buffer = new byte[s.Length];            
try
{
    s.Read(buffer, 0, (Int32)s.Length);
}            
finally 
{ 
    s.Close();
}     

Note:

  • The code works fine
  • I’m using ASP.NET 4.0 with C#
  • IIS version is 7.5
  • 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-16T00:14:41+00:00Added an answer on June 16, 2026 at 12:14 am

    This is from http://support.microsoft.com/kb/306158

    public const int LOGON32_LOGON_INTERACTIVE=2;
    public const int LOGON32_PROVIDER_DEFAULT=0;
    
    WindowsImpersonationContext impersonationContext;
    
    [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);
    
    private bool impersonateValidUser(String userName, String domain, String password) {
      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;
    }
    
    private void undoImpersonation() {
      impersonationContext.Undo();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to read the pdf file from raw folder if devices have any
I want to read .ppt files from the iPhone mail app. I have already
I need to read the location of the Temporary ASP.NET Files folder from VBScript
So I'm making this android application, that needs to read data from user-provided CSV
I am developing a program that need read/write some files from sdcard or internal
I want to save files from an external server into a folder on my
I want to read a file from a java web application. I don't want
I want to read a INI file from python, and I came up with
I want to read an .xml file from a web radio for my app.
I want to read the file path from html input type=file (the entry selected

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.