15648

Validate a users credentials on the local machine

Question:

I have a Windows Service (running as the Local System user) that needs to validate a user based on username and password, in addition to checking if the user belongs to the group WSMA. My current code is like this:

var pc = new PrincipalContext(ContextType.Machine); using (pc) { try { if (pc.ValidateCredentials(username, password)) { using (var groupEntry = new DirectoryEntry("WinNT://./WSMA,group")) { foreach (object member in (IEnumerable)groupEntry.Invoke("Members")) { using (var memberEntry = new DirectoryEntry(member)) { if (memberEntry.Path.ToLower().EndsWith(username.ToLower())) { return new LoginResult{ success = true }; } } } } } return new LoginResult{ success = false }; } catch (PrincipalOperationException poe) { if (poe.ErrorCode == -2147023688) { return new LoginResult { Success = false, ErrorMessage = "Password expired" }; } throw poe; } }

This all works as it should, as long as I'm connected to the network, but if I plug out my network cable, then the ValidateCredentials call give me the following error message:

<em><strong>FileNotFoundException unhandeled by user code. The network path was not found.</strong></em>

I guess this has something to do with AD, but I only need to check the local users, and not domain users so a network access should not be required.

Any way to do this using the PrincipalContext, or some other way that will work in a disconnected scenario?

Answer1:

Here's a way to logon the User (and thus check that it's a valid user/pass): <a href="http://msdn.microsoft.com/en-us/library/system.security.principal.windowsimpersonationcontext.aspx" rel="nofollow">MSDN Link</a>

I guess this should work disconnected, too, if you use a local account

Recommend

  • Why is PrincipalContext.ValidateCredentials validating against old credentials?
  • Check if user is part of administrator group - C#
  • How do I reset password retry count for a user in the gpu using C#
  • Getting errors while using Win32 library functions in my Ada code
  • Undefined index 'PWD' in $_server
  • connect active directory using c#
  • Validate a users credentials on the local machine
  • Need all users detail (Name, Email, Designation, Department) in the current organisation using C#
  • Active directory : get groups where a user is member
  • System.DirectoryServices.AccountManagement.PrincipalContext and Impersonation in a WCF service
  • Set cookie from Web Api 2 IAuthenticationFilter AuthenticateAsync method
  • Enumerating Controls on a Form
  • Extract All Possible Paths from Expression-Tree and evaluate them to hold TRUE
  • Trying to string.Join an IList
  • XSLT foreach repeating nodes to flat
  • Cannot get the UserManager class
  • Linq Merge lists
  • SetWindowsHookEx does not react on media keys
  • List images(01.png) and descriptions(01.txt) from directory
  • Graphics.CopyFromScreen [Web application] + The handle is invalid
  • ADO and msqli connections very slow
  • Set the selected item in dropdownlist in MVC3
  • D3 nodes and links from JSON with nested arrays of children
  • javaw.exe and eclipse startup problems
  • output of program is not same as passed argument
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • AT Commands to Send SMS not working in Windows 8.1
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Trying to get generic when generic is not available
  • How do I configure my settings file to work with unit tests?
  • Is it possible to post an object from jquery to bottle.py?