Link to home
Start Free TrialLog in
Avatar of cintra
cintra

asked on

Accessing a file on a password protected domain

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

Avatar of Avodah
Avodah
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of cintra
cintra

ASKER

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

Place this code within the web.config under configuration


   


of course substitute the valid username and password
Avatar of cintra

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Avodah
Avodah
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of cintra

ASKER

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

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.
Avatar of cintra

ASKER

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
Can you post exact code in which you are trying impersonation?
Avatar of cintra

ASKER

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

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cintra

ASKER

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??

Avatar of cintra

ASKER

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!!??