1

C# or VB.NET suggestion is welcome.

I have computers joined to a domain. I'm writing a desktop application that ask for a username and password to authenticate user against Active Directory. Sometimes, user uses this application on the computer that is not joined to the domain.

I'm using .NET 3.5, System.DirectoryServices, and System.DirectoryServices.AccountManagement. Code sample how to authenticate users:

Private Function ValidateExternalUser(ByVal username As String, ByVal password As String) As Boolean
    Using context As PrincipalContext = New PrincipalContext(ContextType.Domain, "your_domain_here")
        Return context.ValidateCredentials(username, password, ContextOptions.Negotiate)
    End Using
End Function

' from http://stackoverflow.com/questions/30861/authenticating-domain-users-with-system-directoryservices

I want to know how to check if user is already logged in on domain computer, then I don't have to ask them log into the application again.

Update

If it can't be done with System.DirectoryServices.AccountManagemen, is there any way to do it? Thanks

3 Answers 3

2

If the machine is not attached to the domain, then the System.Environment.DomainName property will be equal to the System.Environment.MachineName.

2
  • I like this advise. But I cannot find 'System.Environment.DomainName' property. Instead, I found 'System.Environment.UserDomainName'. It is a misprint in answer? I m running on NET 4.0. Mar 29, 2018 at 18:31
  • 1
    Yes. .NET 2.0 was the base for my answer. It's the same though.
    – Rick Rat
    Apr 23, 2019 at 23:56
0

I don't think you can do that with the S.DS.AM namespace. Knowing whether or not a user is logged into a domain would be something that needs to be handled on the domain controller at runtime - S.DS.AM is concerned with static information (user's properties), not really dynamic runtime properties (who's logged in).

1
  • If it can't be done with System.DirectoryServices.AccountManagemen, is there any way to do it? Thanks
    – Narazana
    Nov 9, 2010 at 6:25
0

I checked System.Security.Principal.WindowsIdentity.GetCurrent.Name , and it gives me

"domain\username"

With that information and System.Security.Principal.WindowsIdentity.GetCurrent.IsAuthenticated , I think I get what I want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.