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

Narazana
  • 1,940
  • 15
  • 57
  • 88

3 Answers3

2

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

Rick Rat
  • 1,732
  • 1
  • 16
  • 32
  • 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. – Boris Zinchenko Mar 29 '18 at 18:31
  • 1
    Yes. .NET 2.0 was the base for my answer. It's the same though. – Rick Rat Apr 23 '19 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).

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • If it can't be done with System.DirectoryServices.AccountManagemen, is there any way to do it? Thanks – Narazana Nov 09 '10 at 06: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.

Narazana
  • 1,940
  • 15
  • 57
  • 88