Click here to Skip to main content
12,302,514 members (65,411 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

28.7K views
10 bookmarked
Posted

User Impersonation in Windows Forms Application

, 3 Aug 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
User Impersonation in Windows Forms Application

Impersonation in ASP.NET is easy as you can do it from the web.config as given below:

<identity impersonate="true" />

Doing this would impersonate the IIS authenticating user on every request for every page in your ASP.NET application.

You can also impersonate a specific user like such:

<identity impersonate="true" userName="username" password="password" />

That’s the easy part, but if you want it to be doing it only on specific parts of the application, it's where the fun begins.

Now doing it in Windows Forms Application is similar to doing it in ASP if you want to specify a specific user to do specific tasks on your application. Now why would you want to do that? I guess you need to perform specific tasks on a machine with different identity context, for example you have 10 remote machines not on your domain with 20 different admin users not on Active Directory and you want to perform File Copy operation on the C$ drive. Doing this manually would be a great deal of copy and pasting and logging in as a specific user.

Now here is the class to do the impersonation for a specific user which you can use on certain parts of your code.

using System;
using System.Security.Principal;
using System.Runtime.InteropServices;

public static class Impersonate
{
public static WindowsImpersonationContext oImpersonatedUser;

[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool LogonUser
	(string sUsername, string sDomain, string sPassword, 
	int iLogonType, int iLogonProvider, ref IntPtr oToken);

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private unsafe static extern int FormatMessage
	(int iFlags, ref IntPtr oSource, int iMessageId, 
	int iLanguageId, ref String sBuffer, int iSize, IntPtr* Arguments);

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool CloseHandle(IntPtr oHandle);

[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public extern static bool DuplicateToken
	(IntPtr oExistingTokenHandle, int SECURITY_IMPERSONATION_LEVEL, 
	ref IntPtr oDuplicateTokenHandle);

// Log On Types
const int LOGON32_LOGON_INTERACTIVE = 2;
const int LOGON32_LOGON_NETWORK = 3;
const int LOGON32_LOGON_NEW_CREDENTIALS = 9;

// Log On Providers
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_PROVIDER_WINNT35 = 1;
const int LOGON32_PROVIDER_WINNT40 = 2;
const int LOGON32_PROVIDER_WINNT50 = 3;

const int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100;
const int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200;
const int FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000;

/// <span class="code-SummaryComment"><summary>
</span>

And this is how you use it.

Impersonate.ImpersonateNow(sServerIP, 
	sUserName, sPassword);//Do your own stuff hereImpersonate.ImpersonateEnd();

Now one more final note. Since you are using Pointers and Fixed Size Buffers, it means you have to use the method in an unsafe context which means by default if you debug the application you will be having an error message of “Unsafe code may only appear if compiling with /unsafe”.  You can change the Project Build Properties to ignore the error or allow the unsafe code.


License

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

Share

About the Author

Raymund Macaalay
Technical Lead
New Zealand New Zealand
http://nz.linkedin.com/in/macaalay
http://macaalay.com/

You may also be interested in...

Comments and Discussions

 
QuestionHelp With Windows 7 Pin
jishnusygal20-Sep-12 19:06
memberjishnusygal20-Sep-12 19:06 
QuestionThanks, you save my life Pin
yeya8a4-Aug-11 7:42
memberyeya8a4-Aug-11 7:42 
GeneralWorks for .NET libs but not so good with Excel COM Interop library Pin
Jonathan Kosbab15-Jun-10 8:42
memberJonathan Kosbab15-Jun-10 8:42 
GeneralRe: Works for .NET libs but not so good with Excel COM Interop library Pin
Member 45143508-Feb-12 6:37
memberMember 45143508-Feb-12 6:37 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160530.1 | Last Updated 4 Aug 2010
Article Copyright 2010 by Raymund Macaalay
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid