I wrote a service that monitors directories all over my network. As the company grows, my service is becoming more and more utilized. I now have to monitor drives and devices through VPN tunnels on different domains. Up to this point all my clients have been willing to add in my service account user into there domain to allow me access. I have just acquired a new client that will not add in this user to their domain (I can not blame them). This client has provided me a username and password to access their provided network share.
I tried adding in a WMI connection using this user, not being an admin, the connection fails.
My next idea was to use the LogonUser API to impersonate users. This also fails because I have no access to the DC on the remote network. When my code tries to get the SecurityToken by authenticating with the machine it is running on. The user can not authenticate because the user is not on my domain (user is from the clients domain).
Leading into my question:
Is there a way to have the Windows Authentication Prompt* auto fill itself with a provided username/password?
* The dialog box that prompts for username and password when you try to access a network share that your user does not have access to.
Thanks for any help!
Code used for Logon 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 Integer) As Boolean
Const LOGON32_LOGON_INTERACTIVE As Long = 2
Const LOGON32_LOGON_NETWORK As Long = 3
Const LOGON32_PROVIDER_DEFAULT As Long = 0
Const LOGON32_PROVIDER_WINNT50 As Long = 3
Const LOGON32_PROVIDER_WINNT40 As Long = 2
Const LOGON32_PROVIDER_WINNT35 As Long = 1
Private Function GetWindowsIdentity(ByVal UserName As String, _
ByVal Domain As String, ByVal Password As String) As WindowsIdentity
Dim SecurityToken As Integer
Dim Success As Boolean
Success = LogonUser(UserName, Domain, Password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, SecurityToken)
GetWindowsIdentity = New WindowsIdentity(New IntPtr(SecurityToken))
End Function
Sub subConnect()
Dim strUser, strPass, strDomain As String
Dim intType, intProvider As Integer
Dim intToken As Integer
strUser = strXMLUser
strPass = strXMLPass
strDomain = strXMLDom
intType = LOGON32_LOGON_NETWORK
intProvider = LOGON32_PROVIDER_DEFAULT
Dim NewIdentity As WindowsIdentity
Dim CurIdentity As WindowsIdentity
Try
NewIdentity = GetWindowsIdentity(strUser, strDomain, strPass)
NewContext = NewIdentity.Impersonate()
Dim tmpindent As WindowsIdentity
tmpindent = WindowsIdentity.GetCurrent()
strDa01 = String.Format("[" & DateTime.Now & "]" & " Login as: " & tmpindent.Name)
strData = strData.Concat(strData, strDa01)
strDa01 = Environment.NewLine
strData = strData.Concat(strData, strDa01)
Catch ex As Exception
strDa01 = String.Format("[" & DateTime.Now & "]" & " Error - " & ex.Message)
strData = strData.Concat(strData, strDa01)
strDa01 = Environment.NewLine
strData = strData.Concat(strData, strDa01)
End Try
End Sub
Is it possible I am using the LogonUser API wrong?
Looking around the old VB6 way was: SetWindowText and FindWindow
Does anyone know the .net equivalent?
I tried adding in a WMI connection using this user, not being an admin, the connection fails.
My next idea was to use the LogonUser API to impersonate users. This also fails because I have no access to the DC on the remote network. When my code tries to get the SecurityToken by authenticating with the machine it is running on. The user can not authenticate because the user is not on my domain (user is from the clients domain).
Leading into my question:
Is there a way to have the Windows Authentication Prompt* auto fill itself with a provided username/password?
* The dialog box that prompts for username and password when you try to access a network share that your user does not have access to.
Thanks for any help!
Code used for Logon 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 Integer) As Boolean
Const LOGON32_LOGON_INTERACTIVE As Long = 2
Const LOGON32_LOGON_NETWORK As Long = 3
Const LOGON32_PROVIDER_DEFAULT As Long = 0
Const LOGON32_PROVIDER_WINNT50 As Long = 3
Const LOGON32_PROVIDER_WINNT40 As Long = 2
Const LOGON32_PROVIDER_WINNT35 As Long = 1
Private Function GetWindowsIdentity(ByVal UserName As String, _
ByVal Domain As String, ByVal Password As String) As WindowsIdentity
Dim SecurityToken As Integer
Dim Success As Boolean
Success = LogonUser(UserName, Domain, Password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, SecurityToken)
GetWindowsIdentity = New WindowsIdentity(New IntPtr(SecurityToken))
End Function
Sub subConnect()
Dim strUser, strPass, strDomain As String
Dim intType, intProvider As Integer
Dim intToken As Integer
strUser = strXMLUser
strPass = strXMLPass
strDomain = strXMLDom
intType = LOGON32_LOGON_NETWORK
intProvider = LOGON32_PROVIDER_DEFAULT
Dim NewIdentity As WindowsIdentity
Dim CurIdentity As WindowsIdentity
Try
NewIdentity = GetWindowsIdentity(strUser, strDomain, strPass)
NewContext = NewIdentity.Impersonate()
Dim tmpindent As WindowsIdentity
tmpindent = WindowsIdentity.GetCurrent()
strDa01 = String.Format("[" & DateTime.Now & "]" & " Login as: " & tmpindent.Name)
strData = strData.Concat(strData, strDa01)
strDa01 = Environment.NewLine
strData = strData.Concat(strData, strDa01)
Catch ex As Exception
strDa01 = String.Format("[" & DateTime.Now & "]" & " Error - " & ex.Message)
strData = strData.Concat(strData, strDa01)
strDa01 = Environment.NewLine
strData = strData.Concat(strData, strDa01)
End Try
End Sub
Is it possible I am using the LogonUser API wrong?
Looking around the old VB6 way was: SetWindowText and FindWindow
Does anyone know the .net equivalent?