Click here to Skip to main content
13,348,831 members (79,350 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


17 bookmarked
Posted 7 Sep 2010

Back to Basic – ASP.NET Runtime Impersonation

, 7 Sep 2010
Rate this:
Please Sign up or sign in to vote.
In this post, I’ll explain how you can make ASP.NET impersonation and in more details how to make runtime impersonation.

Today, I got a question from one of the developers at my main customer. The question was how to move an uploaded file from an ASP.NET server to a file server on the network. The answer is of course by impersonating. In this post, I’ll explain how you can make ASP.NET impersonation and in more details, how to make runtime impersonation.

Impersonation in ASP.NET

When we are doing I/O operations, the operation system makes security checks to understand if the user is authorized to do the operation. The same thing happens when you try to do operations on another machine in your network. Impersonation in ASP.NET occurs when ASP.NET executes code in the context of an authenticated and authorized user. By default, ASP.NET runs in the ASPNET account. By using impersonation, we can impersonate the ASPNET account to another account that has access to resources which aren’t granted in the internet security permission. One way to impersonate a user is by using the identity element in the web.config. When you use the following code in your web.config, ASP.NET impersonates to the authenticated user or to an anonymous internet user account:

<identity impersonate="true" />

If you want to impersonate to a specific user, you can use the following configuration:

<identity impersonate="true"  

userName="domain\username" password=" password"/>

Runtime Impersonation

At my customer, the previous configuration examples weren’t an option. The second way to impose impersonation is by runtime. This option can be achieved by using the System.Security.Principal and the WindowsIdentity class. The WindowsIdentity class has a method that makes impersonation and returns a WindowsImpersonationContext. The problem with this class is that you need to supply to it an IntPtr which is a security access token of the user that you wish to impersonate. The solution is to use P/Invoke and call the LogonUser Win32 API. After you get the impersonation context, you can run the network operations that you seek to perform. After you finish doing your operations, you need to undo the impersonation. The following code shows an example of an impersonation service class:

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

namespace WebApplication1
    public class ImpersonationService
        #region Consts

        public const int LOGON32_LOGON_INTERACTIVE = 2;
        public const int LOGON32_PROVIDER_DEFAULT = 0;


        #region External API 
        [DllImport("advapi32.dll", SetLastError = true)]        
        public static extern int LogonUser(
            string lpszUsername, string lpszDomain,
            string lpszPassword, int dwLogonType,
            int dwLogonProvider, out IntPtr phToken
        [DllImport("advapi32.dll", SetLastError = true)]        
        public static extern bool RevertToSelf();

        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern int CloseHandle(IntPtr hObject); 


        #region Methods 

        public void PerformImpersonatedTask(string username, string domain,
            string password, int logonType, int logonProvider, Action methodToPerform)
            IntPtr token = IntPtr.Zero;
            if (RevertToSelf())
                if (LogonUser(username, domain, password, logonType,
                    logonProvider, out token) != 0)
                    var identity = new WindowsIdentity(token);
                    var impersonationContext = identity.Impersonate();
                    if (impersonationContext != null)
                    // do logging
            if (token != IntPtr.Zero)

Here is an example of how to use the class in your ASP.NET application:

using System;
using System.IO;

namespace WebApplication1
    public partial class _Default : System.Web.UI.Page
        #region Page Events

        private void Page_Load(object sender, System.EventArgs e)
            var service = new ImpersonationService();
            service.PerformImpersonatedTask("username", "domain", "password",
                new Action(MethodToPerform));

        #region Methods 
        public void MethodToPerform()
            var serverPath = @"\\ServerName\test";            
            var dirInfo = new DirectoryInfo(serverPath);



In the post, I showed a simple way to implement a class that impersonates to a relevant account in order to achieve some functionality that internet security permissions don’t allow. You should consider using the web.config instead since it does all the communication with Win32 API instead of the supplied code. The impersonation isn’t limited only to ASP.NET and can also be used in other frameworks.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Gil Fink
Technical Lead sparXys
Israel Israel
Gil Fink is a web development expert and ASP.Net/IIS Microsoft MVP. He is the founder and owner of sparXys. He is currently consulting for various enterprises and companies, where he helps to develop Web and RIA-based solutions. He conducts lectures and workshops for individuals and enterprises who want to specialize in infrastructure and web development. He is also co-author of several Microsoft Official Courses (MOCs) and training kits, co-author of "Pro Single Page Application Development" book (Apress) and the founder of Front-End.IL Meetup. You can read his publications at his website:

You may also be interested in...

Comments and Discussions

QuestionTrouble Pin
Member 85283468-Mar-12 11:21
memberMember 85283468-Mar-12 11:21 
GeneralMy vote of 5 Pin
ambarishtv28-Jan-12 21:52
memberambarishtv28-Jan-12 21:52 
QuestionAlternative Runtime Impersonation Pin
Uwe Keim7-Jul-11 1:29
sitebuilderUwe Keim7-Jul-11 1:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 7 Sep 2010
Article Copyright 2010 by Gil Fink
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid