File.Copy

51 views
Skip to first unread message

Pratiksha Saxena

unread,
Nov 17, 2009, 6:49:49 AM11/17/09
to dotnetde...@googlegroups.com
Hi,

i have a web application in which on button_click i have written code for File.Copy

 File.Copy(@"e:\Text.txt", @"\\okh-lb-labs-030\\D$\\MT\\Text.txt"); 


but i gives error Logon failure: unknown user name or bad password.

The files are in Intranet.
Source is   e:\Text.txt
Destination is \okh-lb-labs-030\\D$\\MT\\Text.txt

If i copy on my local system it is fine but on network it gives error.i tried even giving rights to MT folder but no use.

Please help.

Pratiksha

slim MOURALI

unread,
Nov 17, 2009, 11:32:36 AM11/17/09
to dotnetde...@googlegroups.com
you must give permission on the remote machine... to the user that the iis
is running

2009/11/17 Pratiksha Saxena <pratiksh...@gmail.com>

Jamie Fraser

unread,
Nov 18, 2009, 6:25:15 AM11/18/09
to dotnetde...@googlegroups.com
IIS runs as a specific user.

That user must have permissions on the file share.

Keidrick Pettaway

unread,
Nov 26, 2026, 8:29:59 AM (now) Nov 26
to dotnetde...@googlegroups.com
You need to assign permissions on the directory itself for the said user. I would also make the user(s) also sign in to hit the file. What kind of authentication method are you currently using?

Keidrick Pettaway
Web:
http://www.kpettaway.com
Twitter:
http://twitter.com/kpett
Linkedin:
http://www.linkedin.com/pub/keidrick-pettaway/8/705/b58


From: Jamie Fraser <jamie....@gmail.com>
Date: Wed, 18 Nov 2009 11:25:15 +0000
Subject: Re: [DotNetDevelopment] File.Copy

Pratiksha Saxena

unread,
Nov 18, 2009, 1:28:45 AM11/18/09
to dotnetde...@googlegroups.com
What permission should i give.I have already given permission in folder for the files to be copied to the user.
Eg my machine name is okh-lb-labs-014 i have given rights to okh-lb-labs-014 as full control.What else should i give.

Thanks
Pratiksha

slim MOURALI

unread,
Nov 18, 2009, 11:20:12 AM11/18/09
to dotnetde...@googlegroups.com
permission in IIS server

2009/11/18 Pratiksha Saxena <pratiksh...@gmail.com>

Pratiksha Saxena

unread,
Nov 20, 2009, 12:25:48 AM11/20/09
to dotnetde...@googlegroups.com
Finally i solved this.


Thanks to all for ur suggestions

Pratiksha

Raghupathi Kamuni

unread,
Nov 20, 2009, 3:43:02 AM11/20/09
to dotnetde...@googlegroups.com
How? Let us know the solution. It would be helpful for those looking for solution facing similar problems.

ashish srivastava

unread,
Nov 20, 2009, 7:43:16 AM11/20/09
to dotnetde...@googlegroups.com
when u run it using dot net ide it will work becoz asp.net have a restricted user on IIS but when u user it with
ie it will not it require a user u have to provide it in web config file.
 
ashish

Gayathry

unread,
Nov 18, 2009, 11:08:59 PM11/18/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi,

Pls see the link
http://bytes.com/topic/asp-net/answers/755714-file-copy-remote-machine-probelm

Thanks
Gayathry

On Nov 18, 11:28 am, Pratiksha Saxena <pratiksha.sax...@gmail.com>
wrote:
> What permission should i give.I have already given permission in folder for
> the files to be copied to the user.
> Eg my machine name is okh-lb-labs-014 i have given rights to okh-lb-labs-014
> as full control.What else should i give.
>
> Thanks
> Pratiksha
>
> On Tue, Nov 17, 2009 at 10:02 PM, slim MOURALI <mourali.s...@gmail.com>wrote:
>
>
>
> > you must give permission on the remote machine... to the user that the iis
> > is running
>
> > 2009/11/17 Pratiksha Saxena <pratiksha.sax...@gmail.com>
>
> > Hi,
>
> >> i have a web application in which on button_click i have written code for
> >> File.Copy
>
> >>  File.Copy(@"e:\Text.txt", @"\\okh-lb-labs-030\\D$\\MT\\Text.txt");
>
> >> but i gives error *Logon failure: unknown user name or bad password.*
> >> *
> >> *
> >> *The files are in Intranet.*

Pratiksha Saxena

unread,
Nov 20, 2009, 4:25:15 AM11/20/09
to dotnetde...@googlegroups.com
For benefit of all facing similar problem-for File.Copy in Lan(Use Shared Resource)

For this i impersonated the user who can access the network.

 public const int LOGON32_LOGON_INTERACTIVE = 2;
    public const int LOGON32_PROVIDER_DEFAULT = 0;

    WindowsImpersonationContext impersonationContext;

    [DllImport("advapi32.dll")]
    public static extern int LogonUserA(String lpszUserName,
        String lpszDomain,
        String lpszPassword,
        int dwLogonType,
        int dwLogonProvider,
        ref IntPtr phToken);
    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern int DuplicateToken(IntPtr hToken,
        int impersonationLevel,
        ref IntPtr hNewToken);

    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool RevertToSelf();

    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    public static extern bool CloseHandle(IntPtr handle);



 protected void Page_Load(object sender, EventArgs e)
 if (impersonateValidUser("XXXXXXX", "XXXX", "XXXX"))
        {
           

            //File.Copy(@"\\XXX\XX.XX", @"\\YYYY\YY\YY.YY");
            undoImpersonation();
        }
        else
        {
            //Your impersonation failed. Therefore, include a fail-safe mechanism here.
        }


private bool impersonateValidUser(String userName, String domain, String password)
    {
        WindowsIdentity tempWindowsIdentity;
        IntPtr token = IntPtr.Zero;
        IntPtr tokenDuplicate = IntPtr.Zero;

        if (RevertToSelf())
        {
            if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
                LOGON32_PROVIDER_DEFAULT, ref token) != 0)
            {
                if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
                {
                    tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                    impersonationContext = tempWindowsIdentity.Impersonate();
                    if (impersonationContext != null)
                    {
                        CloseHandle(token);
                        CloseHandle(tokenDuplicate);
                        return true;
                    }
                }
            }
        }
        if (token != IntPtr.Zero)
            CloseHandle(token);
        if (tokenDuplicate != IntPtr.Zero)
            CloseHandle(tokenDuplicate);
        return false;
    }

    private void undoImpersonation()
    {
        impersonationContext.Undo();
    }






Pratiksha
330.gif

Kira Qian

unread,
Jul 31, 2011, 9:30:00 PM7/31/11
to dotnetde...@googlegroups.com

Anna, you do not need to be that complex. e:\Text.txt should be the file on your web app server. The remote server okh-lb-labs-030 should have a share folder and assign Full permission for your IIS server. Your web app use IIS_IUsers account to run the code. If you give full permission to IIS_IUsers account, there should be no problem. Give full permission to “Everyone” should also work and less security. 

Reply all
Reply to author
Forward
0 new messages