?
Solved

Accessing a file on a password protected domain

Posted on 2009-02-20
15
Medium Priority
?
1,218 Views
Last Modified: 2012-05-06
Hi,

I'm building a bit of software that will be accessing files that are hosted on MS Share point. When I map a network drive (Z:\....) and access the file using this path - it all works fine. Problem is when I try to put the network path (\\Cintranet\....) it falis with an exception of 'Logon Failed'

My guess is that when i'm using the network drive Windows automatically sends a usrname and password to gain access... Question is how do I retrieve this information programatically and send this to the Server to gain access?

Any help would be great!!
//This one fails with 'Logon failed' exception
FileInfo theSourceFile = new FileInfo(@"\\\\cintranet\\Operations\\Cintra HR Implementation\\ClientList.ini");
 
//This one works but only on my computer as other people will not have this drive mapping
FileInfo theSourceFile = new FileInfo(@"Z:\Cintra HR Implementation\\ClientList.ini");
 
StreamReader stream = theSourceFile.OpenText();

Open in new window

0
Comment
Question by:cintra
  • 7
  • 5
  • 3
15 Comments
 
LVL 18

Expert Comment

by:Richard Lee
ID: 23690286
Try using impersonation where the user you are using has access to that domain.

http://www.codeproject.com/KB/aspnet/UNC__Access_Network_file.aspx
0
 

Author Comment

by:cintra
ID: 23690432
Thanks, that looks like it nearly does the trick - only problem is I'm using C# and for some reason can't get the syntax right??

See below for what I'm doing

Any ideas????

using System.Web;
...
...
 
namespace CustomisationManagement
{
 
 
    public partial class Customisation_Management : Form
    {
      
        Configuration.identitySection.Impersonate = true;

Open in new window

0
 
LVL 18

Expert Comment

by:Richard Lee
ID: 23690541
Place this code within the web.config under configuration


   


of course substitute the valid username and password
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cintra
ID: 23690653
When you say place this code in the web.config what do you mean? I'm building a Windows application using C# do you mean I should edit System.Web.Configuration dll?

Are we talking about the same thing?
0
 
LVL 18

Accepted Solution

by:
Richard Lee earned 1000 total points
ID: 23690680
Sorry made. I assumed you were building a web app. My mistake. Here is another option

http://csharptuning.blogspot.com/2007/06/impersonation-in-c.html

and you may also want to look at this

http://mikehadlow.blogspot.com/2007/01/easy-impersonation.html
0
 
LVL 5

Expert Comment

by:sumitkchawla
ID: 23690761
You will have to enable following policies for the user which is trying to access the network resource.

Open Gpedit.msc->Computer Configuration->Windows Settings->Security Settings->Local Policies->User Rights Assignment

And add your user to following policies:

Log on as batch job

Also use LOGON32_LOGON_NETWORK flag in LogOnUser function
0
 

Author Comment

by:cintra
ID: 23691074
Thanks that worked... but its still not doing what I need - this is probably my fault for not understanding what I need. That code allows me to impersonate other users and do things on my local machine.

Problem is when I try to access a file using UNC path the Server that is holding the file still doesn't recognise the process is being carried out a real user - hence its failing.

So... I need to impersonate the current user to the server at the point I try and access the file

I've re-attached my original code to explain..

Thanks

//This one fails with 'Logon failed' exception
FileInfo theSourceFile = new FileInfo(@"\\\\cintranet\\Operations\\Cintra HR Implementation\\ClientList.ini");
 
//This one works but only on my computer as other people will not have this drive mapping
FileInfo theSourceFile = new FileInfo(@"Z:\Cintra HR Implementation\\ClientList.ini");
 
StreamReader stream = theSourceFile.OpenText();

Open in new window

0
 
LVL 5

Expert Comment

by:sumitkchawla
ID: 23691133
Does the user account that you are using for impersonation has permissions on that file on the server? In other words does the same account exist there on the server.

The quickest way to make things work is to have a standard low rights user on Server on the user. and have a similar user on your user machine with same password. This will make life easier for you.
0
 

Author Comment

by:cintra
ID: 23691139
Yeah the user that i'm logged in as has full access to the server. Thats why it works when using a mapped drive, I guess windows automatically logs on to the server and what I want to do is replicate that when using a UNC path
0
 
LVL 5

Expert Comment

by:sumitkchawla
ID: 23691199
Can you post exact code in which you are trying impersonation?
0
 

Author Comment

by:cintra
ID: 23691229
This is what i'm trying... runs ok but I still cant get access to that remote file - this is obviously abbreviated to exclude non impersonation specific stuff

namespace CustomisationManagement
{
 
 
    public partial class Customisation_Management : Form
    {
        IntPtr accessToken = new IntPtr();
      
        // Using this api to get an accessToken of specific Windows User by its user name and password
        [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        static public extern bool LogonUser(string userName, string domain, string passWord, int logonType, int logonProvider, ref IntPtr accessToken);
 
...
...
...
        public void PopulateListBox()
        {
            LogonUser("ned.mcevoy", "CHOUSE", "Pa$$w0rd4", 2, 0, ref accessToken);
            WindowsIdentity identity = new WindowsIdentity(accessToken);
            WindowsImpersonationContext context = identity.Impersonate();
           
//Try to get file here

Open in new window

0
 
LVL 5

Expert Comment

by:sumitkchawla
ID: 23691291
You are using LOGON32_LOGON_INTERACTIVE( = 2) in LogOnUser for dwLogonType parameter. Did you try using LOGON32_LOGON_NETWORK( = 3) ?
Use value 3 for parameter number 4
0
 
LVL 5

Assisted Solution

by:sumitkchawla
sumitkchawla earned 1000 total points
ID: 23691306
Or alternativly try LOGON32_LOGON_BATCH
0
 

Author Comment

by:cintra
ID: 23691355
Interesting.. When using 3 I'm getting a different error

"The network path was not found".-- I removed the double backslashes of the variable and put in Windows Explorer and it does exist

And when using 4 or LOGON32_LOGON_BATCH I get...

"Token cannot be zero." -- reffering to the access token - It musn't be getting set to a value when I pass it onto the function...

Any ideas??

0
 

Author Comment

by:cintra
ID: 23691584
OK... Its just started working????? Without any kind of impersonation I can now get the file using a UNC path - I haven't got a clue what caused it but thank you all for your help and sorry your time was a bit wasted!!??
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

850 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