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, C开发者_运维百科ontextOptions.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
If the machine is not attached to the domain, then the System.Environment.DomainName property will be equal to the System.Environment.MachineName.
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).
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.
精彩评论