?
Solved

How to access UNC path programmatically

Posted on 2009-02-15
11
Medium Priority
?
4,693 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
0
Comment
Question by:PantoffelSlippers
  • 6
  • 5
11 Comments
 
LVL 19

Accepted Solution

by:
daveamour earned 1500 total points
ID: 23644062
0
 

Author Comment

by:PantoffelSlippers
ID: 23644136
Looks interesting Dave

Let me have a look.

Post back soon

Thank you

Cheers
0
 

Author Comment

by:PantoffelSlippers
ID: 23645025
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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 19

Expert Comment

by:daveamour
ID: 23647568
No ideas sorry - I just assumed the code would work!
0
 

Author Comment

by:PantoffelSlippers
ID: 23652199
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
 
LVL 19

Expert Comment

by:daveamour
ID: 23653184
Ahh ok well yes that makes sense
0
 

Author Comment

by:PantoffelSlippers
ID: 24095278
No workaround yet...

Any ideas?
0
 
LVL 19

Expert Comment

by:daveamour
ID: 24095284
Nope sorry
0
 

Author Closing Comment

by:PantoffelSlippers
ID: 31547056
Thank you!
0
 

Author Comment

by:PantoffelSlippers
ID: 24277965
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
 
LVL 19

Expert Comment

by:daveamour
ID: 24277981
Ok glad to help, mpr.dll sounds interesting - shall have a Google...
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month17 days, 3 hours left to enroll

864 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