Solved

vb windows authentication

Posted on 2003-11-11
10
251 Views
Last Modified: 2010-05-01
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
0
Comment
Question by:Raggy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
10 Comments
 
LVL 26

Accepted Solution

by:
EDDYKT earned 125 total points
ID: 9721791
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
 

Author Comment

by:Raggy
ID: 9722857
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
 
LVL 26

Expert Comment

by:EDDYKT
ID: 9722886
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:EDDYKT
ID: 9722891
What error you get by the way? On which line?
Show me the code you had
0
 

Author Comment

by:Raggy
ID: 9723035
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
 
LVL 26

Expert Comment

by:EDDYKT
ID: 9723067
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
 

Author Comment

by:Raggy
ID: 9723181
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
 
LVL 26

Expert Comment

by:EDDYKT
ID: 9723408
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 10537187
Moderator, my recommended disposition is:

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

Dan Rollins -- EE database cleanup volunteer
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help me. 3 79
MsgBox 4 76
VBA: loop recent folder and copy txt file. 8 56
Email Excel WB by selecting name from a dropdown  list to Outlook. 5 47
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question