How to access UNC path programmatically

Hi

I'm trying to access an UNC path through my source code.

The code fails because I haven't accessed the UNC path manually and provided a username and password.  I do have the correct username and password.

How do I provide these in my source code?

Thanks
PantoffelSlippersAsked:
Who is Participating?
 
PantoffelSlippersAuthor Commented:
Looks interesting Dave

Let me have a look.

Post back soon

Thank you

Cheers
0
 
PantoffelSlippersAuthor Commented:
Dave

I had a look.  I created a small application with the code provided but I can't get it to work.

The code runs but some of the authentication fails.

Please see the attached code snippet.

The username etc are correct

I tested manually.

Any ideas?

Thanks

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Security.Principal;
 
 
 
 
namespace WindowsFormsApplication1
{
 
    
 
 
    public partial class Form1 : Form
    {
 
 
        [DllImport("advapi32.dll", SetLastError = true)]
        private static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken);
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        private extern static bool CloseHandle(IntPtr handle);
 
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            IntPtr token;
            WindowsIdentity wi;
 
            //THIS JUMPS INTO THE ELSE WITH ERROR CODE 1326
            if (LogonUser ("Administrator", "SARSTEST", "frikkie",     
                8, // LOGON32_LOGON_NETWORK_CLEARTEXT    
                0, // LOGON32_PROVIDER_DEFAULT    
                out token)){    
                
                wi = new WindowsIdentity (token);    
                WindowsImpersonationContext wic = wi.Impersonate ();    
                File.Copy (@"C:\Production.jpg", @"\\SARSDC01\Shared\");
 
 
                wic.Undo ();
                CloseHandle (token);
}
else
{
    //THIS PART GETS EXECUTED AFTER THE IF
    MessageBox.Show( ("LogonUser() failed with error code " + Marshal.GetLastWin32Error ()));
 
}
 
        }
    }
}

Open in new window

0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
daveamourCommented:
No ideas sorry - I just assumed the code would work!
0
 
PantoffelSlippersAuthor Commented:
Dave,

It seems to me as if the code fails if the machine on which it is run is not on the domain...

When I move the code to a machine that is on the domain, it works.

Let me see if I can work around that

Thanks
0
 
daveamourCommented:
Ahh ok well yes that makes sense
0
 
PantoffelSlippersAuthor Commented:
No workaround yet...

Any ideas?
0
 
daveamourCommented:
Nope sorry
0
 
PantoffelSlippersAuthor Commented:
Thank you!
0
 
PantoffelSlippersAuthor Commented:
Thanks Dave,

The suggested solution works in most circumstances.

I've seen an excellent article on some other Windows DLL (mpr.dll I think....) which seems to work pretty well..   Just a shame that I can't recall where I saw that!

Thanks for the help
0
 
daveamourCommented:
Ok glad to help, mpr.dll sounds interesting - shall have a Google...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.