windows - C# Check if Current Logged on user is Admin (Remote Machine) -
i know there several discussions subject none reply exact question. i'm looking method check remotely if current logged on user has admin rights. whether he's fellow member of local built in "administrators" grouping of machine or fellow member of nested grouping within "administrators", such "domain admins" example. found couple of methods each provides half solution.
method #1 (work remotely checks local "administrators" group):
private bool isadmin() { arraylist mem2 = new arraylist(); string hostname = basicinfomodel.loggedusername; //a username class seek { using (directoryentry machine = new directoryentry("winnt://" + mycomputer.mycomputername)) // remote computer class { //get local admin grouping using (directoryentry grouping = machine.children.find("administrators", "group")) { //get members of local admin grouping object members = group.invoke("members", null); foreach (object fellow member in (ienumerable)members) { //get business relationship name string accountname = new directoryentry(member).name; mem2.add(new directoryentry(member).name); } } } } grab (exception ex) { // grab } if (mem2.contains(hostname.toupper()) || mem2.contains(hostname.tolower())) homecoming true; else homecoming false; }
method #2 (check both local , domain admin privileges not working remotely)
static bool isadmin() { windowsidentity user = new windowsidentity(@"user01"); windowsprincipal princ = new windowsprincipal(user); homecoming princ.isinrole(windowsbuiltinrole.administrator); }
so said, did not find method reply both needs.
check if user has admin rights do remotelythanks help!
well, think found way that, i'm sharing in case other people want utilize it. played couple of methods found , created next (seems working)
static bool isadmin(string username, string machinename) { using (principalcontext ctxmacine = new principalcontext(contexttype.machine, machinename)) { using (principalcontext ctxdomain = new principalcontext(contexttype.domain)) { userprincipal = userprincipal.findbyidentity(ctxdomain, identitytype.samaccountname, username); groupprincipal gp = groupprincipal.findbyidentity(ctxmacine, "administrators"); foreach (userprincipal usr in gp.getmembers(true)) { if (up != null) { if (up.samaccountname.toupper() == usr.samaccountname.toupper()) { homecoming true; } } } } } homecoming false; }
c# windows privileges
No comments:
Post a Comment