Search code examples
c#vb.netactive-directorydirectoryservices

Check if a user is already logged into domain with System.DirectoryServices.AccountManagemen


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


Solution

  • 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.