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!!
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();
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????
See below for what I'm doing
Any ideas????
using System.Web;
...
...
namespace CustomisationManagement
{
public partial class Customisation_Management : Form
{
Configuration.identitySection.Impersonate = true;
Place this code within the web.config under configuration
of course substitute the valid username and password
of course substitute the valid username and password
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?
Are we talking about the same thing?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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
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();
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.
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.
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?
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
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
Use value 3 for parameter number 4
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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??
"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??
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!!??
http://www.codeproject.com/KB/aspnet/UNC__Access_Network_file.aspx