473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

FileCopy to a shared folder

When I try the following:

System.IO.File.Copy("C:\\test_read\\test.txt",
"\\\\192.168.0.5\\test_write\\test.txt", false)

I get an UnauthorizedAccessException.

I cannot however, seem to find out how to authorize the file copy (ie.
process the user/pass).

I'm sure there must be a way I can do this without resorting to writing a
server application that saves a transmitted file over tcp/ip to a local
folder...

Anyone?
Nov 16 '05 #1
10 6239
Try doing a "net use" on that UNC to see whether you have access to it
first.. also check write access. This is probably an issue with your NT user
permissions.

--
John Wood
Blog: http://dotnetjunkies.com/weblog/johnwood
"BLiTZWiNG" <BL*******@discussions.microsoft.com> wrote in message
news:A5**********************************@microsof t.com...
When I try the following:

System.IO.File.Copy("C:\\test_read\\test.txt",
"\\\\192.168.0.5\\test_write\\test.txt", false)

I get an UnauthorizedAccessException.

I cannot however, seem to find out how to authorize the file copy (ie.
process the user/pass).

I'm sure there must be a way I can do this without resorting to writing a
server application that saves a transmitted file over tcp/ip to a local
folder...

Anyone?

Nov 16 '05 #2
Sorry I should have elaborated first. The shared folder is located on a
Win2k3 server equipped with SQL server of which I am the administrator of.

Part of my solution for my client is for an application to upload documents
to the server as well as member information and other related data to the SQL
server. Without resorting to a server application, knowing that my clients
are not confident with more advanced technology (ie FTP), I figured use of a
secure shared folder to which my application will copy the documents to would
be the simplest solution in terms of both the client skill level and
development time.

However, appliaction programming in C#/.NET is new to me, and after
perusings System.IO and System.Security.Permissions I still can't seem to
find a way to request authentication from the server.

"John Wood" wrote:
Try doing a "net use" on that UNC to see whether you have access to it
first.. also check write access. This is probably an issue with your NT user
permissions.

--
John Wood
Blog: http://dotnetjunkies.com/weblog/johnwood
"BLiTZWiNG" <BL*******@discussions.microsoft.com> wrote in message
news:A5**********************************@microsof t.com...
When I try the following:

System.IO.File.Copy("C:\\test_read\\test.txt",
"\\\\192.168.0.5\\test_write\\test.txt", false)

I get an UnauthorizedAccessException.

I cannot however, seem to find out how to authorize the file copy (ie.
process the user/pass).

I'm sure there must be a way I can do this without resorting to writing a
server application that saves a transmitted file over tcp/ip to a local
folder...

Anyone?


Nov 16 '05 #3
if you need to use credentials other than that of the person currently
logged in to the workstation, then you need to look at the
WindowsIdentity.Impersonate method
http://msdn.microsoft.com/library/en...classtopic.asp

HTH

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"BLiTZWiNG" <BL*******@discussions.microsoft.com> wrote in message
news:E5**********************************@microsof t.com...
Sorry I should have elaborated first. The shared folder is located on a
Win2k3 server equipped with SQL server of which I am the administrator of.

Part of my solution for my client is for an application to upload documents to the server as well as member information and other related data to the SQL server. Without resorting to a server application, knowing that my clients
are not confident with more advanced technology (ie FTP), I figured use of a secure shared folder to which my application will copy the documents to would be the simplest solution in terms of both the client skill level and
development time.

However, appliaction programming in C#/.NET is new to me, and after
perusings System.IO and System.Security.Permissions I still can't seem to
find a way to request authentication from the server.

"John Wood" wrote:
Try doing a "net use" on that UNC to see whether you have access to it
first.. also check write access. This is probably an issue with your NT user permissions.

--
John Wood
Blog: http://dotnetjunkies.com/weblog/johnwood
"BLiTZWiNG" <BL*******@discussions.microsoft.com> wrote in message
news:A5**********************************@microsof t.com...
When I try the following:

System.IO.File.Copy("C:\\test_read\\test.txt",
"\\\\192.168.0.5\\test_write\\test.txt", false)

I get an UnauthorizedAccessException.

I cannot however, seem to find out how to authorize the file copy (ie.
process the user/pass).

I'm sure there must be a way I can do this without resorting to writing a server application that saves a transmitted file over tcp/ip to a local folder...

Anyone?


Nov 16 '05 #4
Thanks Nick, though I can see an issue with LogonUser in that it cannot
handle logins to remote computers, which is what I need.

It's the same situation as if you were on 192.168.0.45 and wanted to use the
uploads share of 192.168.0.2. Windows will prompt you for a password
(provided the policies are set as such) and if your authentication succeeds,
you'll be allowed to access it.

I need whatever function it is that sends this remote authentication request.

"Nick Malik [Microsoft]" wrote:
if you need to use credentials other than that of the person currently
logged in to the workstation, then you need to look at the
WindowsIdentity.Impersonate method
http://msdn.microsoft.com/library/en...classtopic.asp

HTH

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"BLiTZWiNG" <BL*******@discussions.microsoft.com> wrote in message
news:E5**********************************@microsof t.com...
Sorry I should have elaborated first. The shared folder is located on a
Win2k3 server equipped with SQL server of which I am the administrator of.

Part of my solution for my client is for an application to upload

documents
to the server as well as member information and other related data to the

SQL
server. Without resorting to a server application, knowing that my clients
are not confident with more advanced technology (ie FTP), I figured use of

a
secure shared folder to which my application will copy the documents to

would
be the simplest solution in terms of both the client skill level and
development time.

However, appliaction programming in C#/.NET is new to me, and after
perusings System.IO and System.Security.Permissions I still can't seem to
find a way to request authentication from the server.

"John Wood" wrote:
Try doing a "net use" on that UNC to see whether you have access to it
first.. also check write access. This is probably an issue with your NT user permissions.

--
John Wood
Blog: http://dotnetjunkies.com/weblog/johnwood
"BLiTZWiNG" <BL*******@discussions.microsoft.com> wrote in message
news:A5**********************************@microsof t.com...
> When I try the following:
>
> System.IO.File.Copy("C:\\test_read\\test.txt",
> "\\\\192.168.0.5\\test_write\\test.txt", false)
>
> I get an UnauthorizedAccessException.
>
> I cannot however, seem to find out how to authorize the file copy (ie.
> process the user/pass).
>
> I'm sure there must be a way I can do this without resorting to writing a > server application that saves a transmitted file over tcp/ip to a local > folder...
>
> Anyone?


Nov 16 '05 #5

"BLiTZWiNG" <BL*******@discussions.microsoft.com> wrote in message
news:7D**********************************@microsof t.com...
Thanks Nick, though I can see an issue with LogonUser in that it cannot
handle logins to remote computers, which is what I need.

It's the same situation as if you were on 192.168.0.45 and wanted to use
the
uploads share of 192.168.0.2. Windows will prompt you for a password
(provided the policies are set as such) and if your authentication
succeeds,
you'll be allowed to access it.

I need whatever function it is that sends this remote authentication
request.

"Nick Malik [Microsoft]" wrote:


No you don't need to "handle logins" to remote computers, what you need is a
"user token" that is valid to access a remote resource, and that is exactly
what LogonUser provides you.
Call LogonUser specifying explicit (cleartext) credentials of a remote (or
domain) account, if LogonUser succeeds you will get a token back that can be
used to impersonate the account when accessing the remote folder.
When running on XP or higher you can even get a token to access the network
only without touching the current token.

Willy.

Nov 16 '05 #6


"BLiTZWiNG" <BL*******@discussions.microsoft.com> wrote in message
news:4C**********************************@microsof t.com...
Thanks Willy. I decided to continue pursuing this course.

However I'm still getting error 1326 (Logon failure: unknown user name or
bad password) even though I can go straight to start->run and type
\\ip.address or domain.name\share and be granted access after entering
"Administrator" and the password, so I know the share is working and I
have
access to it.

Why then would LogonUser not recognise the password? Would it have
something
to do with Active Directory? My workstation is not a part of the domain
I'm
trying to access, and for all intents and purposes, the final system wont
necessarily be a domain controller or part of a domain, it's just a
machine
on a network that I know the user/pass of.
So here is my LogonUser("Administrator", "192.168.0.5", "password", 8, 0,
out token);

I get a 1326 from that. Any ideas?

1326 - Means that "administrator" and/or "password" is not valid on
192.168.0.5. Or simply the credentials supplied are not valid.
Could you post your code (or at least the LogonUser declaration and the
calling code part).

Willy.
PS. LogonUser has nothing to do with AD.
Nov 16 '05 #7
Ok, here is the code as it stands currently.
Note that "password" has been replaced for seurity reasons.
Also note that I'm using LOGON32_LOGON_NEW_CREDENTIALS (9) now which threw a
SecurityException "Unable to impersonate user" instead of
LOGON32_LOGON_NETWORK_CLEARTEXT (8) which gave the 1326.

private void button1_Click(object sender, System.EventArgs e)
{
int token;
WindowsIdentity wid_current = WindowsIdentity.GetCurrent();

kernel.LogonUser("Administrator", "192.168.0.5", "password", 9, 0, out token);
int ret = kernel.GetLastError();
if (ret != 0) { MessageBox.Show(ret.ToString(), "Error"); return; }

// Code after this doesn't get executed yet

IntPtr admin_token = new IntPtr(token);
WindowsIdentity wid_admin = new WindowsIdentity(admin_token);
WindowsImpersonationContext wic = wid_admin.Impersonate();

System.IO.File.Copy("C:\\test_read\\test.txt",
"\\\\192.168.0.5\\trent\\test.txt", true);
wic.Undo();
}

I would love to assume that I'm getting somewhere now that I'm getting a
securityexception.

Thanks for your help so far Willy.
BLiTZWiNG.

"Willy Denoyette [MVP]" wrote:


"BLiTZWiNG" <BL*******@discussions.microsoft.com> wrote in message
news:4C**********************************@microsof t.com...
Thanks Willy. I decided to continue pursuing this course.

However I'm still getting error 1326 (Logon failure: unknown user name or
bad password) even though I can go straight to start->run and type
\\ip.address or domain.name\share and be granted access after entering
"Administrator" and the password, so I know the share is working and I
have
access to it.

Why then would LogonUser not recognise the password? Would it have
something
to do with Active Directory? My workstation is not a part of the domain
I'm
trying to access, and for all intents and purposes, the final system wont
necessarily be a domain controller or part of a domain, it's just a
machine
on a network that I know the user/pass of.

So here is my LogonUser("Administrator", "192.168.0.5", "password", 8, 0,
out token);

I get a 1326 from that. Any ideas?

1326 - Means that "administrator" and/or "password" is not valid on
192.168.0.5. Or simply the credentials supplied are not valid.
Could you post your code (or at least the LogonUser declaration and the
calling code part).

Willy.
PS. LogonUser has nothing to do with AD.

Nov 16 '05 #8
Ok, I see your mistake...

Never use this:
int ret = kernel.GetLastError();

use
int ret = Marshal.GetLastWin32Error()
and ONLY call it when LogonUser fails (returns 0), else you'll pick up
whatever last error was set (as you do).

[DllImport("advapi32.dll")]
public static extern int LogonUser(String lpszUsername, String lpszDomain,
String lpszPassword,
int dwLogonType, int dwLogonProvider, outIntPtr phToken);

if(LogonUser(....) != 0)
{
}
else // LogonUser failed, get error number
{
int ret = Marshal.GetLastWin32Error() ;
....
}

Willy.

"BLiTZWiNG" <BL*******@discussions.microsoft.com> wrote in message
news:86**********************************@microsof t.com...
Ok, here is the code as it stands currently.
Note that "password" has been replaced for seurity reasons.
Also note that I'm using LOGON32_LOGON_NEW_CREDENTIALS (9) now which threw
a
SecurityException "Unable to impersonate user" instead of
LOGON32_LOGON_NETWORK_CLEARTEXT (8) which gave the 1326.

private void button1_Click(object sender, System.EventArgs e)
{
int token;
WindowsIdentity wid_current = WindowsIdentity.GetCurrent();

kernel.LogonUser("Administrator", "192.168.0.5", "password", 9, 0, out
token);
int ret = kernel.GetLastError();
if (ret != 0) { MessageBox.Show(ret.ToString(), "Error"); return; }

// Code after this doesn't get executed yet

IntPtr admin_token = new IntPtr(token);
WindowsIdentity wid_admin = new WindowsIdentity(admin_token);
WindowsImpersonationContext wic = wid_admin.Impersonate();

System.IO.File.Copy("C:\\test_read\\test.txt",
"\\\\192.168.0.5\\trent\\test.txt", true);
wic.Undo();
}

I would love to assume that I'm getting somewhere now that I'm getting a
securityexception.

Thanks for your help so far Willy.
BLiTZWiNG.

"Willy Denoyette [MVP]" wrote:


"BLiTZWiNG" <BL*******@discussions.microsoft.com> wrote in message
news:4C**********************************@microsof t.com...
> Thanks Willy. I decided to continue pursuing this course.
>
> However I'm still getting error 1326 (Logon failure: unknown user name
> or
> bad password) even though I can go straight to start->run and type
> \\ip.address or domain.name\share and be granted access after entering
> "Administrator" and the password, so I know the share is working and I
> have
> access to it.
>
> Why then would LogonUser not recognise the password? Would it have
> something
> to do with Active Directory? My workstation is not a part of the domain
> I'm
> trying to access, and for all intents and purposes, the final system
> wont
> necessarily be a domain controller or part of a domain, it's just a
> machine
> on a network that I know the user/pass of.
>

> So here is my LogonUser("Administrator", "192.168.0.5", "password", 8,
> 0,
> out token);
>
> I get a 1326 from that. Any ideas?

1326 - Means that "administrator" and/or "password" is not valid on
192.168.0.5. Or simply the credentials supplied are not valid.
Could you post your code (or at least the LogonUser declaration and the
calling code part).

Willy.
PS. LogonUser has nothing to do with AD.

Nov 16 '05 #9
Ahh! It's been so long since I used functions like GetLastError...

Ok, now I'm getting the 126 error, even when I specifiy the full path to
advapi32.dll, or copy the file to the local working directory.

This looks like it should be a common error going by the docs actually
putting this error in...

"Willy Denoyette [MVP]" wrote:
Ok, I see your mistake...

Never use this:
int ret = kernel.GetLastError();

use
int ret = Marshal.GetLastWin32Error()
and ONLY call it when LogonUser fails (returns 0), else you'll pick up
whatever last error was set (as you do).

[DllImport("advapi32.dll")]
public static extern int LogonUser(String lpszUsername, String lpszDomain,
String lpszPassword,
int dwLogonType, int dwLogonProvider, outIntPtr phToken);

if(LogonUser(....) != 0)
{
}
else // LogonUser failed, get error number
{
int ret = Marshal.GetLastWin32Error() ;
....
}

Willy.

"BLiTZWiNG" <BL*******@discussions.microsoft.com> wrote in message
news:86**********************************@microsof t.com...
Ok, here is the code as it stands currently.
Note that "password" has been replaced for seurity reasons.
Also note that I'm using LOGON32_LOGON_NEW_CREDENTIALS (9) now which threw
a
SecurityException "Unable to impersonate user" instead of
LOGON32_LOGON_NETWORK_CLEARTEXT (8) which gave the 1326.

private void button1_Click(object sender, System.EventArgs e)
{
int token;
WindowsIdentity wid_current = WindowsIdentity.GetCurrent();

kernel.LogonUser("Administrator", "192.168.0.5", "password", 9, 0, out
token);
int ret = kernel.GetLastError();
if (ret != 0) { MessageBox.Show(ret.ToString(), "Error"); return; }

// Code after this doesn't get executed yet

IntPtr admin_token = new IntPtr(token);
WindowsIdentity wid_admin = new WindowsIdentity(admin_token);
WindowsImpersonationContext wic = wid_admin.Impersonate();

System.IO.File.Copy("C:\\test_read\\test.txt",
"\\\\192.168.0.5\\trent\\test.txt", true);
wic.Undo();
}

I would love to assume that I'm getting somewhere now that I'm getting a
securityexception.

Thanks for your help so far Willy.
BLiTZWiNG.

"Willy Denoyette [MVP]" wrote:


"BLiTZWiNG" <BL*******@discussions.microsoft.com> wrote in message
news:4C**********************************@microsof t.com...
> Thanks Willy. I decided to continue pursuing this course.
>
> However I'm still getting error 1326 (Logon failure: unknown user name
> or
> bad password) even though I can go straight to start->run and type
> \\ip.address or domain.name\share and be granted access after entering
> "Administrator" and the password, so I know the share is working and I
> have
> access to it.
>
> Why then would LogonUser not recognise the password? Would it have
> something
> to do with Active Directory? My workstation is not a part of the domain
> I'm
> trying to access, and for all intents and purposes, the final system
> wont
> necessarily be a domain controller or part of a domain, it's just a
> machine
> on a network that I know the user/pass of.
>

> So here is my LogonUser("Administrator", "192.168.0.5", "password", 8,
> 0,
> out token);
>
> I get a 1326 from that. Any ideas?
1326 - Means that "administrator" and/or "password" is not valid on
192.168.0.5. Or simply the credentials supplied are not valid.
Could you post your code (or at least the LogonUser declaration and the
calling code part).

Willy.
PS. LogonUser has nothing to do with AD.


Nov 16 '05 #10


"BLiTZWiNG" <BL*******@discussions.microsoft.com> wrote in message
news:98**********************************@microsof t.com...
Ahh! It's been so long since I used functions like GetLastError...

Ok, now I'm getting the 126 error, even when I specifiy the full path to
advapi32.dll, or copy the file to the local working directory.

This looks like it should be a common error going by the docs actually
putting this error in...

See my reply to another thread (LogonUser issues) you started.

Willy.
Nov 16 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: deko | last post by:
I want to turn error handling off, then back on in a sub routine that backs up a select set of documents. For some reason, I can't seem to get this to work - Private Sub BackupDocs() On Error...
6
by: deko | last post by:
In a multi-user environment, I have a table that stores hyperlinks to documents that are stored on the machine that hosts the mdb database. The table entry looks like this: ...
2
by: Dan Baker | last post by:
I'm relatively new to the .cs world. I would like to have some "shared" .cs files, which several solutions can use. I don't want to place these in a ..dll, I simply want them included into...
2
by: baldwin | last post by:
Hello, We are going to have a web application that will be load balanced. Uploading documents into the same server is not an option. So we need to create a shared folder in a different machine...
0
by: daniele.balducci | last post by:
Hi All, I'm experiencing troubles in a simple .net web application. The applicatione is made up by a single form looping to move some files to a different folder . the main loop is as follows...
6
by: jzdeng | last post by:
Hi, All I use VS 2005 to create a web service. The web service is used to create a sheared folder. It works fine we I run it from VS 2005. But, when I move it to inetpub, it does not work...
0
by: reidarT | last post by:
I have a project with an installfile-folder with files. I want to copy files from this folder to another location like Sorce = "InstallFiles\file.txt" Destination="c:\...\file.txt" FileCopy...
5
by: Bob | last post by:
I'm not sure why this is happening. Using Access 2003. My code is as follows... Private Sub Command1_Click() Dim strSourceFile As String Dim strDestinationFile As String strSourceFile =...
7
by: TracyWants2Know | last post by:
Access Version: 2002 SP3 OS: MS Windows XP Professional Code from Module: Dim txtFileNameBuES As String Dim txtFileNameBuESXP As String Dim txtPathNameBuTo As String ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.