Click here to Skip to main content
15,898,659 members
Home / Discussions / C#
   

C#

 
GeneralRe: URL Validator Pin
camasmartin28-Feb-04 6:48
camasmartin28-Feb-04 6:48 
GeneralOutlook Bar Problem Pin
BigBlob20227-Feb-04 12:49
BigBlob20227-Feb-04 12:49 
GeneralRe: Outlook Bar Problem Pin
Matthew Hazlett27-Feb-04 13:16
Matthew Hazlett27-Feb-04 13:16 
GeneralRe: Outlook Bar Problem Pin
BigBlob20227-Feb-04 13:24
BigBlob20227-Feb-04 13:24 
GeneralExpert only question...please help me Pin
aleang7227-Feb-04 12:34
aleang7227-Feb-04 12:34 
GeneralRe: Expert only question...please help me Pin
Heath Stewart28-Feb-04 3:59
protectorHeath Stewart28-Feb-04 3:59 
QuestionImpersonation??? Pin
Bill Dean27-Feb-04 12:33
Bill Dean27-Feb-04 12:33 
AnswerRe: Impersonation??? Pin
turbochimp27-Feb-04 17:08
turbochimp27-Feb-04 17:08 
Several possibilities. First off, is your impersonation succeeding?

1. Is the ASPNet account set to run as part of the OS? It will need to be, and remember that authentication in a web app is different than logging on locally.
2. I'm not certain what a value of 3 means for LogonType in LogonUser call - I assume it means NETWORK logon, which doesn't create a primary token handle, required for impersonation - should be using INTERACTIVE logon (2).
3. No token is being provided when you create your WindowsIdentity.
4. It may only be in your example, but aren't the forward slashes in the path supposed to be backslashes?
5. Is this running on/against XP or 2000? There are documented issues with getting impersonation to work under 2000 without hacking security to do it.

Hope this helps.


Example I got to work on XP (example hints came directly from MSDN, BTW):
C#
using System;
using System.Security;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Security.Permissions;

namespace ConsoleApplication2
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class Class1
	{
		[DllImport("advapi32.dll", SetLastError=true)]
		public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, 
			int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

		[DllImport("kernel32.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)]
		private unsafe static extern int FormatMessage(int dwFlags, ref IntPtr lpSource, 
			int dwMessageId, int dwLanguageId, ref String lpBuffer, int nSize, IntPtr *Arguments);

		[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
		public extern static bool CloseHandle(IntPtr handle);

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


		// GetErrorMessage formats and returns an error message
		// corresponding to the input errorCode.
		public unsafe static string GetErrorMessage(int errorCode)
		{
			int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100;
			int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200;
			int FORMAT_MESSAGE_FROM_SYSTEM  = 0x00001000;

			int messageSize = 255;
			String lpMsgBuf = "";
			int dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;

			IntPtr ptrlpSource = IntPtr.Zero;
			IntPtr prtArguments = IntPtr.Zero;
        
			int retVal = FormatMessage(dwFlags, ref ptrlpSource, errorCode, 0, ref lpMsgBuf, messageSize, &prtArguments);
			if (0 == retVal)
			{
				throw new Exception("Failed to format message for error code " + errorCode + ". ");
			}

			return lpMsgBuf;
		}

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			const int LOGON32_PROVIDER_DEFAULT = 0;
			const int LOGON32_LOGON_INTERACTIVE = 2;  //This parameter causes LogonUser to create a primary token.
			const int SecurityImpersonation = 2;

			IntPtr token = new IntPtr(0);
			IntPtr dupToken = new IntPtr(0);

			bool result = LogonUser("frog", "pond", "ribbit", LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token);
			if(result == false)
			{
                                //Could use GetErrorMessage call here...
				Console.WriteLine("Failed to log on.");
				Console.ReadLine();
				return;
			}

			bool dupResult = DuplicateToken(token, SecurityImpersonation, ref dupToken);
			if(dupResult == false)
			{
                                //Could use GetErrorMessage call here...
				Console.WriteLine("Failed to log on.");
				Console.ReadLine();
				return;
			}

			WindowsIdentity newId = new WindowsIdentity(dupToken);
			Console.WriteLine("Impersonation starting.  Windows ID name before  = " + WindowsIdentity.GetCurrent().Name);

			WindowsImpersonationContext impersonatedUser = newId.Impersonate();
			Console.WriteLine("Impersonation complete.  Windows ID name after Impersonate = " + WindowsIdentity.GetCurrent().Name);

			impersonatedUser.Undo();
			Console.WriteLine("Impersonation undone.  Windows ID name after Undo  = " + WindowsIdentity.GetCurrent().Name);

			Console.ReadLine();
		}
	}
}

Hope this helps...

The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’

GeneralDataTable problem! Pin
visiontec27-Feb-04 12:03
visiontec27-Feb-04 12:03 
GeneralRe: DataTable problem! Pin
turbochimp27-Feb-04 16:17
turbochimp27-Feb-04 16:17 
Questionhow to use an icon in multiple places Pin
visiontec27-Feb-04 11:43
visiontec27-Feb-04 11:43 
AnswerRe: how to use an icon in multiple places Pin
Heath Stewart28-Feb-04 3:46
protectorHeath Stewart28-Feb-04 3:46 
GeneralIcons Pin
BigBlob20227-Feb-04 10:18
BigBlob20227-Feb-04 10:18 
GeneralRe: Icons Pin
Heath Stewart27-Feb-04 11:27
protectorHeath Stewart27-Feb-04 11:27 
GeneralRe: Icons Pin
BigBlob20227-Feb-04 12:08
BigBlob20227-Feb-04 12:08 
GeneralRe: Icons Pin
Heath Stewart28-Feb-04 3:21
protectorHeath Stewart28-Feb-04 3:21 
Questionwhat is wrong with code?? Pin
SherKar27-Feb-04 10:14
SherKar27-Feb-04 10:14 
AnswerRe: what is wrong with code?? Pin
Heath Stewart27-Feb-04 11:15
protectorHeath Stewart27-Feb-04 11:15 
GeneralRe: what is wrong with code?? Pin
Dave Kreskowiak27-Feb-04 16:43
mveDave Kreskowiak27-Feb-04 16:43 
GeneralRe: what is wrong with code?? Pin
SherKar27-Feb-04 20:55
SherKar27-Feb-04 20:55 
GeneralRe: what is wrong with code?? Pin
Heath Stewart28-Feb-04 3:28
protectorHeath Stewart28-Feb-04 3:28 
GeneralRe: what is wrong with code?? Pin
SherKar27-Feb-04 20:58
SherKar27-Feb-04 20:58 
GeneralPorting to C# Pin
Kant27-Feb-04 9:30
Kant27-Feb-04 9:30 
GeneralRe: Porting to C# Pin
Heath Stewart27-Feb-04 9:49
protectorHeath Stewart27-Feb-04 9:49 
GeneralRe: Porting to C# Pin
Kant27-Feb-04 10:31
Kant27-Feb-04 10:31 

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.