Firstly, you do not want to use the domain name of the computer. You want to use the domain name for a user logs on to the computer. This is due to the fact that some networks can have confidence when the computer is in the same domain, but users from another domain can also log in.
So, to get a domain name of the user logon in Windows, you can use this (example VB.NET, but is easy enough to hide to C #):
Dim arr As String() = My.User.Name.Split("\") If (arr.Length > 0) Then Dim domainName As String = arr(0).ToString Dim userName As String = arr(1).ToString.ToLower End If ( "\") Dim arr As String() = My.User.Name.Split("\") If (arr.Length > 0) Then Dim domainName As String = arr(0).ToString Dim userName As String = arr(1).ToString.ToLower End If
Now, to again check the Windows user account before you run the application, you must call the call Windows API:
Private Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As String, _ ByVal lpszDomain As String, _ ByVal lpszPassword As String, _ ByVal dwLogonType As Integer, _ ByVal dwLogonProvider As Integer, _ ByRef phToken As IntPtr) As Boolean Const LOGON32_LOGON_INTERACTIVE As Long = 2 Const LOGON32_LOGON_NETWORK As Long = 3 Public Function ValidateLogin(ByVal domainName As String, ByVal uid As String, ByVal pwd As String) As Boolean Dim token As IntPtr Return LogonUser(uid, domainName, pwd, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, token) End Function " (ByVal lpszUsername As String, _ Private Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As String, _ ByVal lpszDomain As String, _ ByVal lpszPassword As String, _ ByVal dwLogonType As Integer, _ ByVal dwLogonProvider As Integer, _ ByRef phToken As IntPtr) As Boolean Const LOGON32_LOGON_INTERACTIVE As Long = 2 Const LOGON32_LOGON_NETWORK As Long = 3 Public Function ValidateLogin(ByVal domainName As String, ByVal uid As String, ByVal pwd As String) As Boolean Dim token As IntPtr Return LogonUser(uid, domainName, pwd, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, token) End Function , ByVal uid As String, ByVal pwd As String) As Boolean Private Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As String, _ ByVal lpszDomain As String, _ ByVal lpszPassword As String, _ ByVal dwLogonType As Integer, _ ByVal dwLogonProvider As Integer, _ ByRef phToken As IntPtr) As Boolean Const LOGON32_LOGON_INTERACTIVE As Long = 2 Const LOGON32_LOGON_NETWORK As Long = 3 Public Function ValidateLogin(ByVal domainName As String, ByVal uid As String, ByVal pwd As String) As Boolean Dim token As IntPtr Return LogonUser(uid, domainName, pwd, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, token) End Function , LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, token) Private Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As String, _ ByVal lpszDomain As String, _ ByVal lpszPassword As String, _ ByVal dwLogonType As Integer, _ ByVal dwLogonProvider As Integer, _ ByRef phToken As IntPtr) As Boolean Const LOGON32_LOGON_INTERACTIVE As Long = 2 Const LOGON32_LOGON_NETWORK As Long = 3 Public Function ValidateLogin(ByVal domainName As String, ByVal uid As String, ByVal pwd As String) As Boolean Dim token As IntPtr Return LogonUser(uid, domainName, pwd, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, token) End Function