Solved

How to access file(s) using a UNC path with credentials in VB.NET

Posted on 2009-04-07
10
7,415 Views
Last Modified: 2013-12-02
I'm writing a VB.NET app which needs access to read and write text files in a folder on a remote server.  While I could just map a drive letter on the local machine, this would give anyone who started up Explorer or My Computer access to this folder.  I thought about running an FTP server on the remote server and having the VB program PUT and GET files but it's not as fast as a drive letter mapping.

So I am wondering if it is possible to reference the file(s) simply form a UNC path where I could name the file I want and pass some credentials to gain access to the file.  

Can this be done or does anyone have anothert suggestion?  
0
Comment
Question by:mskvarenina
  • 5
  • 5
10 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24095572
0
 

Author Comment

by:mskvarenina
ID: 24151782
I've looked at the links. I'm still a little confused as to how I Read/Write files on the remote server using the impersonated user and the UNC path.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24155384
The links should provide you plenty information. Some more links. These are related to ASP.NET but the concept is same across web and windows apps.

http://forums.asp.net/p/926183/1075980.aspx#1075980
http://aspalliance.com/336_Upload_Files_Using_ASPNET_Impersonation_and_UNC_Share.all
0
 

Author Comment

by:mskvarenina
ID: 24159650
I have the impersonation working(at least I do not get any errors). When I try to read a file from the remote server using UNC format, I get an error that access is denied. The UNC I use is in the format of
//999.999.999.999/myFolder/myfile.txt.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24159713
Firstly, i think you should use this path
\\999.999.999.999\myfolder\myfile.txt

The error is self explanatory!
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:mskvarenina
ID: 24160988
First, I am using \\999.999.999.999\ format. Sorry about the slashs.

When I do the Impersonate User, the function returns as successfull. No Errors.
The LogonUser function completes with no error.
The user/password exists on both the client and server.

    Public Sub ImpersonateUser(ByVal Impersonate As Boolean)

        Dim newidentity As WindowsIdentity
        If Impersonate = True Then
            newidentity = GetWindowsIdentity(UserName, Domain, Password)
            NewContext = newidentity.Impersonate
        Else
            NewContext.undo()
        End If
    End Sub

    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, Logon.Network, Provider.UserDefault, SecurityToken)

        If Not Success Then
            Throw New System.Exception("Logon Failed. Error: " & _
            GetLastError())
        End If

        GetWindowsIdentity = New WindowsIdentity(New IntPtr(SecurityToken))
        ' Gets to here with no error!!!
    End Function

Dim fileName as String  = "\\999.999.999.999\MyFolder\MyFile.txt"
Dim DataStream As FileStream
Try
      DataStream = New FileStream(fileName, FileMode.Open, FileAccess.Read)
      Catch UnauthorizedAccessEx As UnauthorizedAccessException
      Return ERROR_DATA_FILE_READ_OPEN   'Enum with error #

End Try
 
0
 

Author Comment

by:mskvarenina
ID: 24168790
I understand the error. What I am trying to get is why, after I do the impersonation(If I am doing it correctly), that I still get the access denied error. The user I am impersonating has full access to the remote server, folder and files.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 24168870
Check this link and make sure your code is complete and correct

http://www.codeproject.com/KB/cs/cpimpersonation1.aspx
0
 

Author Comment

by:mskvarenina
ID: 24195915
Thanks for your help. Your last link started me on the correct path. One of the threads from that link pointed to another thread that had the answer. I had all of my code correct, but needed to change permission(s) for the current user on the local machine. The current user must have Act as part of the operating system permission. After changing this, the code worked fine.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24196128
I thought you would accept the answer then.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…

762 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

19 Experts available now in Live!

Get 1:1 Help Now