We help IT Professionals succeed at work.

How to access UNC path programmatically

PantoffelSlippers
on
Medium Priority
4,750 Views
Last Modified: 2013-11-07
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
Comment
Watch Question

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
PantoffelSlippersOperations Manager

Author

Commented:
Looks interesting Dave

Let me have a look.

Post back soon

Thank you

Cheers
PantoffelSlippersOperations Manager

Author

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

No ideas sorry - I just assumed the code would work!
PantoffelSlippersOperations Manager

Author

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
Ahh ok well yes that makes sense
PantoffelSlippersOperations Manager

Author

Commented:
No workaround yet...

Any ideas?
Nope sorry
PantoffelSlippersOperations Manager

Author

Commented:
Thank you!
PantoffelSlippersOperations Manager

Author

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
Ok glad to help, mpr.dll sounds interesting - shall have a Google...
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.