Click here to Skip to main content
15,897,291 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# install wizard ?!! Pin
Kannan Kalyanaraman2-Jul-03 18:56
Kannan Kalyanaraman2-Jul-03 18:56 
GeneralRe: C# install wizard ?!! Pin
apferreira3-Jul-03 4:06
apferreira3-Jul-03 4:06 
QuestionRemoted Window Forms possible? Pin
lykac2-Jul-03 12:33
lykac2-Jul-03 12:33 
GeneralLabel.Text, strings, & spaces. Pin
aadilm2-Jul-03 11:10
aadilm2-Jul-03 11:10 
GeneralRe: Label.Text, strings, & spaces. Pin
dynamic2-Jul-03 13:38
dynamic2-Jul-03 13:38 
GeneralSending HTTP POST request using System.Net.HttpWebRequest Pin
Le centriste2-Jul-03 10:19
Le centriste2-Jul-03 10:19 
GeneralSolved Pin
Le centriste2-Jul-03 16:12
Le centriste2-Jul-03 16:12 
GeneralUsing LogonUser function of Advapi32.dll Pin
PSharad2-Jul-03 10:03
PSharad2-Jul-03 10:03 
HI All,

I am trying to authenticate my username password using Advapi32.dll with function LogonUser. I am passing my Username and Password and domain and all things which need to pass in this function. But I am always getting false from this function. I am using windows 2000 Prof. and i have installed microsoft visual studio .net 2002 on my system.

One strainge thing is this exe is running on XP box very nicely. So any body tell me what could be the problem?

thanks,
Sharad

My code is:
-------------

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices; // DllImport
using System.Security.Principal; // WindowsImpersonationContext
using System.Security.Permissions; // PermissionSetAttribute

namespace Impersonate
{
// group type enum
public enum SECURITY_IMPERSONATION_LEVEL : int
{
SecurityAnonymous = 0,
SecurityIdentification = 1,
SecurityImpersonation = 2,
SecurityDelegation = 3
}

///
/// Summary description for Form1.
///

public class Form1 : System.Windows.Forms.Form
{

// obtains user token
[DllImport("advapi32.dll", SetLastError=true)]
public static extern bool LogonUser(string pszUsername, string pszDomain, string pszPassword,
int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

// closes open handes returned by LogonUser
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public extern static bool CloseHandle(IntPtr handle);

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

private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label lblCurrentUser;
private System.Windows.Forms.TextBox textBoxUsername;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBoxPassword;
private System.Windows.Forms.Button buttonLogon;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBoxDomain;
private System.Security.Principal.WindowsIdentity newwinid;
private System.Security.Principal.WindowsImpersonationContext newUser;
private System.Windows.Forms.Button buttonRevert;

///
/// Required designer variable.
///

private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//

// set logged on username
lblCurrentUser.Text = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

// populate logon domain name
string sTempUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
if (sTempUser.IndexOf("\\") != -1)
{
string[] aryUser = new String[2];
char[] splitter = {'\\'};
aryUser = sTempUser.Split(splitter);
textBoxDomain.Text = aryUser[0];
}
}

///
/// Clean up any resources being used.
///

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.lblCurrentUser = new System.Windows.Forms.Label();
this.textBoxUsername = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.textBoxPassword = new System.Windows.Forms.TextBox();
this.buttonLogon = new System.Windows.Forms.Button();
this.label4 = new System.Windows.Forms.Label();
this.textBoxDomain = new System.Windows.Forms.TextBox();
this.buttonRevert = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(88, 16);
this.label1.TabIndex = 0;
this.label1.Text = "Running As:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// lblCurrentUser
//
this.lblCurrentUser.Location = new System.Drawing.Point(112, 24);
this.lblCurrentUser.Name = "lblCurrentUser";
this.lblCurrentUser.Size = new System.Drawing.Size(200, 16);
this.lblCurrentUser.TabIndex = 1;
//
// textBoxUsername
//
this.textBoxUsername.Location = new System.Drawing.Point(112, 80);
this.textBoxUsername.Name = "textBoxUsername";
this.textBoxUsername.Size = new System.Drawing.Size(144, 20);
this.textBoxUsername.TabIndex = 2;
this.textBoxUsername.Text = "";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 80);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(88, 16);
this.label2.TabIndex = 3;
this.label2.Text = "New Username:";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// label3
//
this.label3.Location = new System.Drawing.Point(16, 104);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(88, 16);
this.label3.TabIndex = 5;
this.label3.Text = "New Password:";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// textBoxPassword
//
this.textBoxPassword.Location = new System.Drawing.Point(112, 104);
this.textBoxPassword.Name = "textBoxPassword";
this.textBoxPassword.PasswordChar = '*';
this.textBoxPassword.Size = new System.Drawing.Size(144, 20);
this.textBoxPassword.TabIndex = 4;
this.textBoxPassword.Text = "";
//
// buttonLogon
//
this.buttonLogon.Location = new System.Drawing.Point(181, 136);
this.buttonLogon.Name = "buttonLogon";
this.buttonLogon.TabIndex = 6;
this.buttonLogon.Text = "Logon";
this.buttonLogon.Click += new System.EventHandler(this.buttonLogon_Click);
//
// label4
//
this.label4.Location = new System.Drawing.Point(16, 56);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(88, 16);
this.label4.TabIndex = 8;
this.label4.Text = "Logon Domain:";
this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// textBoxDomain
//
this.textBoxDomain.Location = new System.Drawing.Point(112, 56);
this.textBoxDomain.Name = "textBoxDomain";
this.textBoxDomain.Size = new System.Drawing.Size(144, 20);
this.textBoxDomain.TabIndex = 7;
this.textBoxDomain.Text = "";
//
// buttonRevert
//
this.buttonRevert.Enabled = false;
this.buttonRevert.Location = new System.Drawing.Point(104, 136);
this.buttonRevert.Name = "buttonRevert";
this.buttonRevert.TabIndex = 9;
this.buttonRevert.Text = "Revert";
this.buttonRevert.Click += new System.EventHandler(this.buttonRevert_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(312, 189);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.buttonRevert,
this.label4,
this.textBoxDomain,
this.buttonLogon,
this.label3,
this.textBoxPassword,
this.label2,
this.textBoxUsername,
this.lblCurrentUser,
this.label1});
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "Impersonation Example";
this.ResumeLayout(false);

}
#endregion

///
/// The main entry point for the application.
///

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void buttonLogon_Click(object sender, System.EventArgs e)
{
if (textBoxUsername.Text != "" && textBoxPassword.Text != "")
{
try
{
// attempt to impersonate specified user

newUser = this.ImpersonateUser(textBoxUsername.Text, textBoxDomain.Text, textBoxPassword.Text);
// update the running as name
lblCurrentUser.Text = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
buttonRevert.Enabled = true;
buttonLogon.Enabled = false;
}
catch (Exception ex)
{
// why did it fail?
MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
MessageBox.Show(this, "Complete all the logon credentials", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

///
/// Revert back to previous user
///

private void buttonRevert_Click(object sender, System.EventArgs e)
{
// revert to previous user
newUser.Undo();
buttonRevert.Enabled = false;
buttonLogon.Enabled = true;
// update the running as name
lblCurrentUser.Text = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
}

///
/// Attempts to impersonate a user. If successful, returns
/// a WindowsImpersonationContext of the new users identity.
///

/// <param name="sUsername" />Username you want to impersonate
/// <param name="sDomain" />Logon domain
/// <param name="sPassword" />User's password to logon with
/// <returns>
public WindowsImpersonationContext ImpersonateUser(string sUsername, string sDomain, string sPassword)
{
// initialize tokens
IntPtr pExistingTokenHandle = new IntPtr(0);
IntPtr pDuplicateTokenHandle = new IntPtr(0);
pExistingTokenHandle = IntPtr.Zero;
pDuplicateTokenHandle = IntPtr.Zero;

// if domain name was blank, assume local machine
if (sDomain == "")
sDomain = System.Environment.MachineName;

try
{
string sResult = null;

const int LOGON32_PROVIDER_WINNT50 = 2;

// create token
const int LOGON32_LOGON_NEW_CREDENTIALS = 4;
//const int SecurityImpersonation = 2;

// get handle to token
bool bImpersonated = LogonUser(sUsername, sDomain, sPassword,
LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_WINNT50, ref pExistingTokenHandle);

// did impersonation fail?
if (false == bImpersonated)
{
int nErrorCode = Marshal.GetLastWin32Error();
sResult = "LogonUser() failed with error code: " + nErrorCode + "\r\n";

// show the reason why LogonUser failed
MessageBox.Show(this, sResult, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

// Get identity before impersonation
sResult += "Before impersonation: " + WindowsIdentity.GetCurrent().Name + "\r\n";

bool bRetVal = DuplicateToken(pExistingTokenHandle, (int)SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, ref pDuplicateTokenHandle);

// did DuplicateToken fail?
if (false == bRetVal)
{
int nErrorCode = Marshal.GetLastWin32Error();
CloseHandle(pExistingTokenHandle); // close existing handle
sResult += "DuplicateToken() failed with error code: " + nErrorCode + "\r\n";

// show the reason why DuplicateToken failed
MessageBox.Show(this, sResult, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
else
{
// create new identity using new primary token
WindowsIdentity newId = new WindowsIdentity(pDuplicateTokenHandle);
WindowsImpersonationContext impersonatedUser = newId.Impersonate();

// check the identity after impersonation
sResult += "After impersonation: " + WindowsIdentity.GetCurrent().Name + "\r\n";

MessageBox.Show(this, sResult, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
return impersonatedUser;
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
// close handle(s)
if (pExistingTokenHandle != IntPtr.Zero)
CloseHandle(pExistingTokenHandle);
if (pDuplicateTokenHandle != IntPtr.Zero)
CloseHandle(pDuplicateTokenHandle);
}
}
}
}
GeneralRe: Using LogonUser function of Advapi32.dll Pin
rami2k26-May-04 22:55
rami2k26-May-04 22:55 
GeneralRe: Using LogonUser function of Advapi32.dll Pin
Jassim Rahma8-Jul-05 8:30
Jassim Rahma8-Jul-05 8:30 
GeneralRe: Using LogonUser function of Advapi32.dll Pin
rami2k11-Jul-05 23:06
rami2k11-Jul-05 23:06 
GeneralUsing DataGrid with OleDB Pin
Hoang Dung2-Jul-03 9:59
Hoang Dung2-Jul-03 9:59 
GeneralRe: Using DataGrid with OleDB Pin
dynamic2-Jul-03 10:32
dynamic2-Jul-03 10:32 
GeneralRe: Using DataGrid with OleDB Pin
dynamic2-Jul-03 10:33
dynamic2-Jul-03 10:33 
GeneralRe: Using DataGrid with OleDB Pin
dynamic2-Jul-03 10:56
dynamic2-Jul-03 10:56 
GeneralRe: Using DataGrid with OleDB Pin
Khang Nguyen3-Jul-03 4:19
Khang Nguyen3-Jul-03 4:19 
GeneralC# event inheritance Pin
lquest2-Jul-03 8:38
lquest2-Jul-03 8:38 
GeneralRe: C# event inheritance Pin
J. Dunlap2-Jul-03 9:05
J. Dunlap2-Jul-03 9:05 
GeneralProblems with the ComboBox Pin
Torsch2-Jul-03 8:37
Torsch2-Jul-03 8:37 
GeneralRe: Problems with the ComboBox Pin
James T. Johnson2-Jul-03 23:00
James T. Johnson2-Jul-03 23:00 
QuestionCan it really be true? Pin
Marc Clifton2-Jul-03 6:42
mvaMarc Clifton2-Jul-03 6:42 
AnswerRe: Can it really be true? Pin
leppie2-Jul-03 7:09
leppie2-Jul-03 7:09 
GeneralRe: Can it really be true? Pin
Marc Clifton2-Jul-03 7:23
mvaMarc Clifton2-Jul-03 7:23 
GeneralRe: Can it really be true? Pin
Nick Parker2-Jul-03 7:31
protectorNick Parker2-Jul-03 7:31 
GeneralRe: Can it really be true? Pin
leppie2-Jul-03 7:33
leppie2-Jul-03 7:33 

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.