c# - Check current user credentials in debug mode in a wpf application -


i trying run application able stop , start service remotely. application needs able used users without admin privileges impersonating user username, password , domain read encrypted database.

the problem i'm facing i'm not sure if impersonate function working correctly, hence why want check in debug mode user current.

this class use impersonate user:

using system; using system.collections.generic; using system.text; using system.security.principal; using system.runtime.interopservices; using system.security.permissions;  public class impersonateuser {     [dllimport("advapi32.dll", setlasterror = true)]     public static extern bool logonuser(     string lpszusername,     string lpszdomain,     string lpszpassword,     int dwlogontype,     int dwlogonprovider,     ref intptr phtoken);     [dllimport("kernel32.dll", charset = charset.auto)]     public extern static bool closehandle(intptr handle);     private static intptr tokenhandle = new intptr(0);     private static windowsimpersonationcontext impersonateduser;     // if incorporate code dll, sure demand     // runs fulltrust.     [permissionsetattribute(securityaction.demand, name = "fulltrust")]     public void impersonate(string domainname, string username, string password)     {         //try         {             // use unmanaged logonuser function user token             // specified user, domain, , password.             const int logon32_provider_default = 0;         // passing parameter causes logonuser create primary token.         const int logon32_logon_interactive = 2;         tokenhandle = intptr.zero;         // ---- step - 1         // call logonuser obtain handle access token.         bool returnvalue = logonuser(         username,         domainname,         password,         logon32_logon_interactive,         logon32_provider_default,         ref tokenhandle); // tokenhandle - new security token         if (false == returnvalue)         {             int ret = marshal.getlastwin32error();             throw new system.componentmodel.win32exception(ret);         }         // ---- step - 2         windowsidentity newid = new windowsidentity(tokenhandle);         // ---- step - 3         {             impersonateduser = newid.impersonate();         }     } } // stops impersonation public void undo() {     impersonateduser.undo();     // free tokens.     if (tokenhandle != intptr.zero)     {         closehandle(tokenhandle);     }             }         

}

this how i'm trying use it:

impersonateuser iu = new impersonateuser(); iu.impersonate("[domain]","[username]","[password]");  try{     servicecontroller service = new servicecontroller(servicename, remotecomputer);     service.start(); }  iu.undo();  

but i'm not sure if code insert ok or should add else when i'm writing code.

i found looking , it's simple call windowsidentity.getcurrent().name; return user using application , in case you're using impersonate() need pass boolean call so: windowsidentity.getcurrent(true).name; whih return impersonated user.

edit

also add other people may facing same problems since trying check privileges of user on remote machine didn't quite trick. that's why used function

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())                 {                     return true;                 }             }         }     } } return false; 

}

which when used this:

 impersonateuser iu = new impersonateuser();         iu.impersonate(domain, username, password);  string name = windowsidentity.getcurrent(true).name; messagebox.show("currentuser: " + name + " " + isadmin(name, remotecomputer));  iu.undo(); 

will tell if current user you're impersonating has administrator privileges on remotecomputer you're trying access.


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

android - CoordinatorLayout, FAB and container layout conflict -