question

alessandrobelli-3238 avatar image
0 Votes"
alessandrobelli-3238 asked XingyuZhao-MSFT commented

how to access to a windows share with credential (VB.NET)?

I need to access from my code in VB.net (framework 4.5) to a network smb share on an external domain that require authentication with windows credential.
I've tryed with a code like this
Dim theNetworkCredential = New System.Net.NetworkCredential(My.Settings.username, My.Settings.password)

         Dim theNetcache = New CredentialCache

         Dim urx As New Uri("\\192.168.6.5\myshare")

         theNetcache.Add(urx, "Basic", theNetworkCredential)

         Dim theFolders() As String = System.IO.Directory.GetDirectories(My.Settings.share)

but It fail with "unknow user or wrong password"
Any Idea?
Thank you
Alex

dotnet-runtime
· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @alessandrobelli-3238 ,
It seems that you want to connect to network share with credential.
Take a look at the following reference:
How to provide user name and password when connecting to a network share
The code in the reference is C#, and you can convert it to vb. net
If you need further assistance, please let me know.

0 Votes 0 ·

Thank you, I've already read the mentioned article NetworkConnection implementation) , I've tryed and it works.
I hoped to find a solution using managed code, as MSDN reports that System.Net.NetworkCredential should works..not in my case

0 Votes 0 ·

Hi @alessandrobelli-3238 ,
Or try:

 Dim theNetworkCredential = New System.Net.NetworkCredential(My.Settings.username, My.Settings.password, Environment.UserDomainName)
0 Votes 0 ·

why Environment.UserDomainName? Should' I use the remote domain name instead of the local domain?

0 Votes 0 ·

Hi @alessandrobelli-3238 ,
Yes, see if you can solve this problem after adding domain.

0 Votes 0 ·

I've already tryed with and without domain name and make no difference

0 Votes 0 ·

Hi @alessandrobelli-3238 ,
It seems that you need to consider using the unmanaged code which works for you.

0 Votes 0 ·

0 Answers