473,511 Members | 16,258 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# and administrator rights

Hello,
I would like to create an application which would run with administrator
rights when launched by a simple user. Is know the RunAs() command in SHELL,
but the password is still needed.

Isn't there a way, knowing the administrator's password, to
programmaticaly launch my application with those rights ?
Thank you.
Feb 3 '06 #1
2 17288

"Laurent" <mo********@free.fr> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Hello,
I would like to create an application which would run with
administrator rights when launched by a simple user. Is know the RunAs()
command in SHELL, but the password is still needed.

Isn't there a way, knowing the administrator's password, to
programmaticaly launch my application with those rights ?


How are you going to protect the admin password?

Perhaps you could use a normal program and an admin service to do the
(hopefully limited) restricted stuff.
Feb 3 '06 #2
I got this code from CodeProject but don't recall the project name. I
used this code because I needed to impersonate a specific user account
to start and stop a service on a remote machine. Perhaps it will help
you.

using System.Security.Principal;
using System.Runtime.InteropServices;
//the following code executed before you perform your task

if ( ! ImpersonationUtil.Impersonate( _userName, _password, _domain ) )

{
MessageBox.Show("Impersonation failed.");
return;
}

//Perform task as this user here...

//After your task, do this:
ImpersonationUtil.UnImpersonate();
Here is the code for the ImpersonationUtil class:

/// <summary>
/// Impersonate a windows logon.
/// </summary>
public class ImpersonationUtil {

/// <summary>
/// Impersonate given logon information.
/// </summary>
/// <param name="logon">Windows logon name.</param>
/// <param name="password">password</param>
/// <param name="domain">domain name</param>
/// <returns></returns>
public static bool Impersonate( string logon, string password, string
domain ) {
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;

if( LogonUser( logon, domain, password, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, ref token) != 0 ) {

if ( DuplicateToken( token, 2, ref tokenDuplicate ) != 0 ) {
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if ( null != impersonationContext ) return true;
}
}

return false;
}

/// <summary>
/// Unimpersonate.
/// </summary>
public static void UnImpersonate() {
impersonationContext.Undo();
}

[DllImport("advapi32.dll", CharSet=CharSet.Auto)]
public static extern int LogonUser(
string lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken );

[DllImport("advapi32.dll",
CharSet=System.Runtime.InteropServices.CharSet.Aut o,
SetLastError=true)]
public extern static int DuplicateToken(
IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken );

private const int LOGON32_LOGON_INTERACTIVE = 2;
private const int LOGON32_LOGON_NETWORK_CLEARTEXT = 4;
private const int LOGON32_PROVIDER_DEFAULT = 0;
private static WindowsImpersonationContext impersonationContext;
}

Feb 3 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5911
by: Jon Hamel | last post by:
All- I am having a problem where my DB2 administrator account on Windows (db2admin) is losing its local Administrator privledges. This happens randomly throughout the day with no ryhme or...
3
1988
by: ray well | last post by:
hi, are administrator rights needed when installing mdac ? are administrator rights needed when installing .net framework ? thanks, ray
7
1623
by: Pieter | last post by:
Hi, Something strange is happing with my Windowf Forms application (VB.NET 2005): The users had all local Administrator-rights on there pc. We changed it to the 'normal' users, and now they...
15
11037
by: eltonchew | last post by:
Hi, We have an inhouse developed VB.NET Windows application which runs successfully only when the user is given a local administrator rights. Without which, it fails to start. There isn't...
0
1942
by: Keita | last post by:
Hi everyone. I'm using a socket-based application for Windows platforms (using winsock). Everything works fine when trying to connect in Administrator mode, but when I'm in User mode no communication...
1
1548
by: Jack Russell | last post by:
Under XP I used the following code AppDomain.CurrentDomain.SetPrincipalPolicy(Security.Principal.PrincipalPolicy.WindowsPrincipal) Dim MyPrincipal As System.Security.Principal.WindowsPrincipal...
2
1149
by: bvlueberry | last post by:
I have used up al;l of my user accounts. I can`t seem to log back on as administrator. This is getting very very frustrating.. Please help me so that I can get my administrating rights back on my...
5
1959
by: sawilla | last post by:
First, I'm new to Python. I'm getting and error when I run Python 2.5.2 as a regular user in Vista but not when I run Python as an administrator. For example, if I type "import numpy" after I...
2
2791
by: Scott | last post by:
Attempting to install WCF service on stand alone 2003 server as a admin user (not administrator but user "WCF" i.e user added to local admin group). It fails with user error: The description...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7418
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7508
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
446
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.