vb windows authentication

Hi,

I have a little app that copies files to from one location to another.  I've set it up to copy to a server, but it only works for me, because I have a user account on the server machine.  Can someone tell me how to login to the server using a specific account from VB to allow me to copy the files to the server.

I know that it could be done by using mapped network drives, but I don't really want to go down that path.

Anybody able to help?

Raggy
RaggyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

EDDYKTCommented:
Try this

Create a new .bas module and add the following:

Option Explicit
Private Declare Function LogonUser Lib "advapi32.dll" Alias "LogonUserA" (ByVal lpszUsername As String, ByVal lpszDomain As String, ByVal lpszPassword As String, ByVal dwLogonType As Long, ByVal dwLogonProvider As Long, phToken As Long) As Long
Private Declare Function ImpersonateLoggedOnUser Lib "advapi32.dll" (ByVal hToken As Long) As Long
Private Declare Function RevertToSelf Lib "advapi32.dll" () As Long
Private Const LOGON32_PROVIDER_DEFAULT    As Long = 0&
Private Const LOGON32_PROVIDER_WINNT35    As Long = 1&
Private Const LOGON32_LOGON_INTERACTIVE   As Long = 2&
Private Const LOGON32_LOGON_NETWORK       As Long = 3&
Private Const LOGON32_LOGON_BATCH         As Long = 4&
Private Const LOGON32_LOGON_SERVICE       As Long = 5&


Public Function doLogon(ByVal strAdminUser As String, ByVal strAdminPassword As String, ByVal strAdminDomain As String) As Boolean
On Error GoTo DamnErr
    Dim lngTokenHandle As Long
    Dim lngLogonType As Long
    Dim lngLogonProvider As Long
    Dim blnResult As Boolean
    lngLogonType = LOGON32_LOGON_INTERACTIVE
    lngLogonProvider = LOGON32_PROVIDER_DEFAULT
    blnResult = RevertToSelf()
    blnResult = LogonUser(strAdminUser, strAdminDomain, strAdminPassword, _
                                         lngLogonType, lngLogonProvider, _
                                         lngTokenHandle)
    blnResult = ImpersonateLoggedOnUser(lngTokenHandle)
    doLogon = blnResult
    Exit Function
DamnErr:
   Dim sERRORtxt As String
   sERRORtxt = "Error Number: " & Err.Number & vbCrLf & _
               "Description: " & Err.Description & vbCrLf & _
               "Source: " & Err.Source & vbCrLf & _
               "Function: doLogon" & vbCrLf & _
               "Date: " & Now() & vbCrLf & _
               "Input:" & vbCrLf & _
               "  strAdminUser=" & strAdminUser & vbCrLf & _
               "  strAdminPassword=" & strAdminPassword & vbCrLf & _
               "  strAdminDomain=" & strAdminDomain
   App.LogEvent sERRORtxt, vbLogEventTypeError
   Err.Clear
End Function

Public Function doLogoff() As Boolean
On Error GoTo DamnErr
    doLogoff = RevertToSelf()
    Exit Function
DamnErr:
Dim sERRORtxt As String
   sERRORtxt = "Error Number: " & Err.Number & vbCrLf & _
               "Description: " & Err.Description & vbCrLf & _
               "Source: " & Err.Source & vbCrLf & _
               "Function: doLogoff" & vbCrLf & _
               "Date: " & Now()
   App.LogEvent sERRORtxt, vbLogEventTypeError
   Err.Clear
End Function



Call it like:

doLogon "username", "password", "domain-if-needed"
'your code that needs admin rights.
'MAKE SURE TO LOGOFF!
doLogoff
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RaggyAuthor Commented:
Cheers for the help, but I don't seem to be able to get this to work correctly.  I'm getting a path not found error, which I assume is coming because I don't have rights to the folder.  Does the user account need to be part of the administrators group?  I've set up a user on the server and given that individual user full control to the destination folder, but I still get the error, any ideas?
0
EDDYKTCommented:
If you don't have access to the folder, the routine I provided is used to run the program with different user that has permission to access the folder

Just do that

doLogon "username", "password", "domain-if-needed"
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

EDDYKTCommented:
What error you get by the way? On which line?
Show me the code you had
0
RaggyAuthor Commented:
Here's what I used to test if I could log on and see the directory.

    doLogon "myuser", "mypassword", "mydomain IP"
*  destDir.Path = "full unc path to server"
    destDir.Enabled = False
    doLogoff

* = error line

the error is where it trys to access the the path on the server.
0
EDDYKTCommented:
What is destdir?

I would suggest to login into that use and then use windows explorer
Can you able to access the path?

may be destdir doesn't support unc name
0
RaggyAuthor Commented:
destDir is the name of the directory list box that is used to display the destination folder contents.  It does accept the UNC path because the application works fine when using my login credentials.

In order to test the login code you gave me I temporarily denied access to my account on the server and then ran the application using the new user login credentials.

Could that cause a problem?
0
EDDYKTCommented:
Have you tried that too?

>>I would suggest to login into that use and then use windows explorer
>>Can you able to access the path?

Just make sure the new login user can access the unc path
0
DanRollinsCommented:
Moderator, my recommended disposition is:

    Accept EDDYKT's comment(s) as an answer.

Dan Rollins -- EE database cleanup volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.