Solved

vb windows authentication

Posted on 2003-11-11
10
247 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
  • 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now