views:

194

answers:

2

How to use windows authentication (local machine administrator user) in windows application written in c#.

Need is whenever user opens my windows application GUI, it should authenticate local administrator credentials even if User is logged in as Administrator there.

Please help.

Is this windows Impersonation?

+1  A: 

One way is if your users will run as standard account, if you set your manifest file to be run as administrator, then it will prompt for an admin username and password always.

What you're probably looking for though is the LogonUser Win32 API to validate the auth info:

[DllImport("advapi32.dll", SetLastError=true)]
public static extern bool LogonUser(
    string lpszUsername, 
    string lpszDomain, 
    string lpszPassword, 
    int dwLogonType, 
    int dwLogonProvider, 
    out IntPtr phToken
    );
Brian R. Bondy
How would i make it specific to Administrator Users? Just authenticate a valid local administrator user (not domain or any other local user).
Novice
+2  A: 

You can call the LogonUser API method to check a username and password.
You can see the [DllImport] here.

If you want to show a standard username/password prompt, you can call the CredUIPromptForCredentials API function; see also here

EDIT

To check whether the user is an administrator, you can call CheckTokenMembership and check whether the user is in the Administrators group.

Alternatively, you can call NetUserGetInfo level 1 and check whether usri1_priv is USER_PRIV_ADMIN.

You can also use WMI or DirectoryServices.

SLaks
How would i make it specific to Administrator Users? Just authenticate a valid local administrator user (not domain or any other local user).
Novice
You need to check whether the username is in the local Administrators group. http://msdn.microsoft.com/en-us/library/Aa376389%28VS.85%29.aspx
SLaks
To check if user is Admin: I am using User Token (out variable of LogonUser method). here is the code:-WindowsIdentity identity = new WindowsIdentity(hToken);WindowsPrincipal principal = new WindowsPrincipal(identity);return principal.IsInRole(WindowsBuiltInRole.Administrator);
Novice