3

I've an application with a logon screen for users to authenticate themselves against the domain. I've made use of the System.DirectoryServices.AccountManagement PrincipalContext/UserPrincipal classes for this.

                        PrincipalContext domain = new PrincipalContext(ContextType.Domain, "mydomain");
                    if (domain.ValidateCredentials(UserName, Password))
                    {
                        //do stuff
                    }

This works quite well in the vast majority of cases. However, for a few select people, this "domain.ValidateCredentials" method will automatically prompt for a smart card insertion when it finds that the UserName is valid in the domain. Simply closing the prompt again will allow my application to proceed, but I would much rather get rid of it completely.

Smart Card Prompt

I've not had much luck finding a cause/solution for this. Any assistance would be appreciated!

Joran Stoops
  • 153
  • 1
  • 13

2 Answers2

0

I had the same problem also today. The solution that is working for me: adding [System.DirectoryServices.AccountManagement.ContextOptions]'Negotiate' to the ValidateCredentials method:

domain.ValidateCredentials(UserName, Password, [System.DirectoryServices.AccountManagement.ContextOptions]'Negotiate')
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 29 '21 at 11:56
0

Adding ContextOptions.Negotiate to the call to ValidateCredentials does solve the problem because this forces using Kerberos or NTLM with the username and password, bypassing asking for the SmartCard: https://learn.microsoft.com/en-us/dotnet/api/system.directoryservices.accountmanagement.contextoptions?view=dotnet-plat-ext-6.0.

Since a using statement is probably already in the code to reference AccountManagement, it is much more concise code to simply use the enumeration:

domain.ValidateCredentials(UserName, Password, ContextOptions.Negotiate)
Joel Mussman
  • 196
  • 1
  • 5