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
Solved

vb windows authentication

Posted on 2003-11-11
10
249 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB 6.0 printer how to align 6 67
VBA error replacing data 6 40
to transfer string from C lanaguage to VBA 4 72
MS SQL Update query with connected table data 3 38
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

856 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