Monday, July 13, 2009

How to know a user has administrative rights are not:

Pass on the username, domain and password to check whether user has admin rights are not to the below code.

Open a C# Console Application and copy the below code.

[Code]

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Net.Security;
using System.Security.Principal;

namespace WindImperson
{
class Program
{
static void Main(string[] args)
{
WindowsPrincipal wndPrin = LogonUser("username", "domain", "pwd");
Console.WriteLine("Logged on {0}", wndPrin.Identity.Name);
Console.WriteLine(wndPrin.IsInRole(WindowsBuiltInRole.Administrator).ToString());
}
public static WindowsPrincipal LogonUser(string userName, string domain, string password)
{
TcpListener tcpListener = new TcpListener(IPAddress.Loopback, 0);
tcpListener.Start();
WindowsIdentity id = null;
tcpListener.BeginAcceptTcpClient(delegate(IAsyncResult asyncResult)
{
using (NegotiateStream serverSide = new NegotiateStream(
tcpListener.EndAcceptTcpClient(asyncResult).GetStream()))
{
serverSide.AuthenticateAsServer(CredentialCache.DefaultNetworkCredentials,
ProtectionLevel.None, TokenImpersonationLevel.Impersonation);
id = (WindowsIdentity)serverSide.RemoteIdentity;
}
}, null);
using (NegotiateStream clientSide = new NegotiateStream(new TcpClient("localhost",
((IPEndPoint)tcpListener.LocalEndpoint).Port).GetStream()))
{
clientSide.AuthenticateAsClient(new NetworkCredential(userName, password, domain),
"", ProtectionLevel.None, TokenImpersonationLevel.Impersonation);
}
return new WindowsPrincipal(id);
}
}
}


[/ Code]

No comments:

Post a Comment