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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:35:14+00:00 2026-06-07T01:35:14+00:00

I am working on a ASP.NET application using C# that is trying to read

  • 0

I am working on a ASP.NET application using C# that is trying to read some files from a website by logging in and then writing the files on local drive.

I have already passed network credentials to logon to the website by reading the default credentials using –

request.Proxy.Credentials = CredentialCache.DefaultCredentials;

Now i want to store the files on a server directory that requires some credentials to access the location.

Code of logging on to the website –

                string webUrl = "https://web.site.com/";

            string formParams = string.Format("user={0}&password={1}", username, password);

            WebRequest req = WebRequest.Create(webUrl);
            req.ContentType = "application/x-www-form-urlencoded";
            req.Method = "POST";
            req.Proxy.Credentials = CredentialCache.DefaultCredentials;
            byte[] bytes = Encoding.ASCII.GetBytes(formParams);
            req.ContentLength = bytes.Length;
            using (Stream os = req.GetRequestStream())
            {
                os.Write(bytes, 0, bytes.Length);
            }
            WebResponse resp = req.GetResponse();

            cookieHeader = resp.Headers["Set-cookie"];

And the location is \\11.11.11.11\Folder\

How can I pass credentials for accessing that location?
I have learned about impersonation but not getting anything helpful so far. I have credentials that gives access to the location. But how can I do so by using C# code?

Thanks in advance 🙂

  • 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-07T01:35:17+00:00Added an answer on June 7, 2026 at 1:35 am

    You can use the LogonUser API for this. You use the LogonUser function to create a token that represents the WindowsIdentity you want to impersonate. You then create a WindowsIdentity using the token and call Impersonate on the identity. All code that follows runs under the impersonated identity.

    Make sure that you always Undo the WindowsImpersonationContext.

    [DllImport("advapi32.dll", SetLastError=true)]
    public static extern bool LogonUser(string lpszUsername, string lpszDomain, 
                                        string lpszPassword, int dwLogonType, 
                                        int dwLogonProvider, out IntPtr phToken);
    
    [DllImport("advapi32.dll", SetLastError=true)]
    public extern static bool DuplicateToken(
        IntPtr ExistingTokenHandle, int SECURITY_IMPERSONATION_LEVEL,
        out IntPtr DuplicateTokenHandle);
    
    [DllImport("kernel32.dll", SetLastError=true)]
    static extern bool CloseHandle(IntPtr hHandle);
    
    const int LOGON32_LOGON_INTERACTIVE = 2;
    const int LOGON32_PROVIDER_DEFAULT = 0;
    IntPtr hToken;
    IntPtr hTokenDuplicate;
    
    if (LogonUser(username, domain, password,
                  LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, out hToken))
    {
        if (DuplicateToken(hToken, 2, out hTokenDuplicate))
        {
            WindowsIdentity windowsIdentity = new WindowsIdentity(hTokenDuplicate);
            WindowsImpersonationContext impersonationContext =
                windowsIdentity.Impersonate();
            try
            {
                // Access files ...
                // ...
            }
            finally
            {
                impersonationContext.Undo();   
                if (hToken != IntPtr.Zero) CloseHandle(hToken);
                if (hTokenDuplicate != IntPtr.Zero) CloseHandle(hTokenDuplicate);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wroking on a ASP.NET console application using C# that is trying to
I am working on an ASP.NET web forms application that is using Forms Authentication.
I'm working on an application using ASP.NET MVC 1.0 and I'm trying to inject
I'm working on an ASP.NET web application, written using Visual Basic, and I'm trying
I am working on an ASP.NET WebForm application using MVP pattern. For every Web
I ran into this dilemma when working on an ASP.net web application using Web
Here I am working with asp.net web application, I am using 2 facebook plugin
I am working on a web application using ASP.NET 3.5. The application has hundreds
I'm currently working on web application using VB in ASP.NET. Right now I have
I am working on big application build using ASP.NET which was started 1.5 years

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.