Login for application
-
I've created a login dialog box to an application, which works fine, but now the customer wants to change to using a system login to the application to track who's on without needing to create separate login's in the app for each person. Also, the person running the app may or may not be the same person that logged into the computer (most likely not). I've been digging around, and found some promising things - but nothing that quite matched up completely. The NetworkCredential class seemed the most promising, but I'm not going to be accessing any resource, I only need to see if it's a valid login, so I couldn't see how I could use this. The WindowsIdentity class also seemed promising, but I didn't see how that could be used for my purposes either. Any ideas?
----- In the land of the blind, the one eyed man is king.
-
I've created a login dialog box to an application, which works fine, but now the customer wants to change to using a system login to the application to track who's on without needing to create separate login's in the app for each person. Also, the person running the app may or may not be the same person that logged into the computer (most likely not). I've been digging around, and found some promising things - but nothing that quite matched up completely. The NetworkCredential class seemed the most promising, but I'm not going to be accessing any resource, I only need to see if it's a valid login, so I couldn't see how I could use this. The WindowsIdentity class also seemed promising, but I didn't see how that could be used for my purposes either. Any ideas?
----- In the land of the blind, the one eyed man is king.
[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 bool LogonUser() { IntPtr tokenHandle = new IntPtr(0); const int LOGON32_PROVIDER_DEFAULT = 0; const int LOGON32_LOGON_INTERACTIVE = 3; tokenHandle = IntPtr.Zero; // Get the domain to authenticate on from the config string DomainName = ConfigurationManager.AppSettings["Domain"]; try { // Call LogonUser to obtain a handle to an access token. if(LogonUser(txtName.Text, DomainName, txtPwd.Text, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref tokenHandle)) { // Since login succeeded, set the current // principal to the logged in user WindowsIdentity WinIdentity = new WindowsIdentity(tokenHandle); System.Threading.Thread.CurrentPrincipal = new WindowsPrincipal(WinIdentity); return true; } else { return false; } } catch(SystemException) { MessageBox.Show("Domain could not be found. Check network connection."); DialogResult = DialogResult.Abort; } catch(Exception ex) { ExceptionPolicy.HandleException(ex, "Unhandled Exception Policy"); DialogResult = DialogResult.Abort; } finally { // Clean up resources if( tokenHandle != IntPtr.Zero ) CloseHandle(tokenHandle); } }
only two letters away from being an asset
-
[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 bool LogonUser() { IntPtr tokenHandle = new IntPtr(0); const int LOGON32_PROVIDER_DEFAULT = 0; const int LOGON32_LOGON_INTERACTIVE = 3; tokenHandle = IntPtr.Zero; // Get the domain to authenticate on from the config string DomainName = ConfigurationManager.AppSettings["Domain"]; try { // Call LogonUser to obtain a handle to an access token. if(LogonUser(txtName.Text, DomainName, txtPwd.Text, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref tokenHandle)) { // Since login succeeded, set the current // principal to the logged in user WindowsIdentity WinIdentity = new WindowsIdentity(tokenHandle); System.Threading.Thread.CurrentPrincipal = new WindowsPrincipal(WinIdentity); return true; } else { return false; } } catch(SystemException) { MessageBox.Show("Domain could not be found. Check network connection."); DialogResult = DialogResult.Abort; } catch(Exception ex) { ExceptionPolicy.HandleException(ex, "Unhandled Exception Policy"); DialogResult = DialogResult.Abort; } finally { // Clean up resources if( tokenHandle != IntPtr.Zero ) CloseHandle(tokenHandle); } }
only two letters away from being an asset
-
Beautiful! I wasn't quite expecting a full solution, but definite thanks! Works perfectly!
----- In the land of the blind, the one eyed man is king.
I just happened to have it lying around from a recent project.
only two letters away from being an asset