System.IO.FileNotFoundException using file.copy

Discussion in 'ASP .Net' started by Stephen Witter, May 28, 2004.

  1. I had previously posted this on the security ng, but haven't had a hit so I
    was wondering if someone here would be willing to take a stab. I am trying
    to copy a file to a network drive. I can do it on the web server but not
    from a client. Here is the code:

    Dim impersonationContext As
    System.Security.Principal.WindowsImpersonationContext
    Dim currentWindowsIdentity As System.Security.Principal.WindowsIdentity
    currentWindowsIdentity
    =CType(User.Identity,System.Security.Principal.WindowsIdentity)
    impersonationContext = currentWindowsIdentity.Impersonate()
    dim Temp as string="TestFile"
    File.Copy("\\MyServer\Template.doc", "\\MyServer\" & Temp &".doc")

    impersonationContext.Undo()

    I get the error "System.IO.FileNotFoundException: Could not find file
    \\Server\File..." when running it from the [authenticated] client browser.
    I searched everywhere on the web and MSDN on this error but found nothing.
    The error it occurs on the file copy statement The file.copy statement is
    trying to copy a file on a different server than the web server.

    Any help is appreciated.

    P.S. The web server is a backup domain w2k controller also, and the asp.net
    user had to be changed to IWAM_Machine.
     
    Stephen Witter, May 28, 2004
    #1
    1. Advertising

  2. Stephen Witter

    bruce barker Guest

    to copy to another server you need to impersonate a primary token, not an
    impersonation. to quickly test this, on the actual server, run ie and test -
    it should work because the principal will be a primary token, if not, turn
    off anonymous.

    to get around this, your code will need to logon as a domain account with
    enough permission to do the copy.

    -- bruce (sqlwork.com)

    "Stephen Witter" <> wrote in message
    news:#...
    > I had previously posted this on the security ng, but haven't had a hit so

    I
    > was wondering if someone here would be willing to take a stab. I am

    trying
    > to copy a file to a network drive. I can do it on the web server but not
    > from a client. Here is the code:
    >
    > Dim impersonationContext As
    > System.Security.Principal.WindowsImpersonationContext
    > Dim currentWindowsIdentity As

    System.Security.Principal.WindowsIdentity
    > currentWindowsIdentity
    > =CType(User.Identity,System.Security.Principal.WindowsIdentity)
    > impersonationContext = currentWindowsIdentity.Impersonate()
    > dim Temp as string="TestFile"
    > File.Copy("\\MyServer\Template.doc", "\\MyServer\" & Temp &".doc")
    >
    > impersonationContext.Undo()
    >
    > I get the error "System.IO.FileNotFoundException: Could not find file
    > \\Server\File..." when running it from the [authenticated] client browser.
    > I searched everywhere on the web and MSDN on this error but found nothing.
    > The error it occurs on the file copy statement The file.copy statement is
    > trying to copy a file on a different server than the web server.
    >
    > Any help is appreciated.
    >
    > P.S. The web server is a backup domain w2k controller also, and the

    asp.net
    > user had to be changed to IWAM_Machine.
    >
    >
     
    bruce barker, May 28, 2004
    #2
    1. Advertising

  3. The funny thing is I have tried to run the process from every server, even
    the one where the file resides (and where it is being copied to), and they
    all give the same message, that is, that the file does not exist. The only
    place it works is on the web server. Anonymous access is turn off on the web
    site.

    I added the following code to test it:

    Dim FilePermission As New FileIOPermission(FileIOPermissionAccess.AllAccess,
    "\\MyServer\MyShare\SomeDirectory\Template.doc") FilePermission.Assert()
    If System.IO.File.Exists("\\MyServer\MyShare\SomeDirectory\Template.doc")
    then
    response.write("OK User: ") response.write(User.Identity.Name)
    response.end
    Else
    Try
    FilePermission.Demand()
    response.write("Exists But Access Denied , User: ")
    response.write(User.Identity.Name)
    Catch se As Exception
    response.write("Realy doesn't exist")
    End Try
    End If

    It always returns "Exists But Access Denied , User: DOMAIN\User"

    Thanks for your reply though. I know these situations can be caused by alot
    of different factors.


    "bruce barker" <> wrote in message
    news:...
    > to copy to another server you need to impersonate a primary token, not an
    > impersonation. to quickly test this, on the actual server, run ie and

    test -
    > it should work because the principal will be a primary token, if not, turn
    > off anonymous.
    >
    > to get around this, your code will need to logon as a domain account with
    > enough permission to do the copy.
    >
    > -- bruce (sqlwork.com)
    >
    > "Stephen Witter" <> wrote in message
    > news:#...
    > > I had previously posted this on the security ng, but haven't had a hit

    so
    > I
    > > was wondering if someone here would be willing to take a stab. I am

    > trying
    > > to copy a file to a network drive. I can do it on the web server but

    not
    > > from a client. Here is the code:
    > >
    > > Dim impersonationContext As
    > > System.Security.Principal.WindowsImpersonationContext
    > > Dim currentWindowsIdentity As

    > System.Security.Principal.WindowsIdentity
    > > currentWindowsIdentity
    > > =CType(User.Identity,System.Security.Principal.WindowsIdentity)
    > > impersonationContext = currentWindowsIdentity.Impersonate()
    > > dim Temp as string="TestFile"
    > > File.Copy("\\MyServer\Template.doc", "\\MyServer\" & Temp &".doc")
    > >
    > > impersonationContext.Undo()
    > >
    > > I get the error "System.IO.FileNotFoundException: Could not find file
    > > \\Server\File..." when running it from the [authenticated] client

    browser.
    > > I searched everywhere on the web and MSDN on this error but found

    nothing.
    > > The error it occurs on the file copy statement The file.copy statement

    is
    > > trying to copy a file on a different server than the web server.
    > >
    > > Any help is appreciated.
    > >
    > > P.S. The web server is a backup domain w2k controller also, and the

    > asp.net
    > > user had to be changed to IWAM_Machine.
    > >
    > >

    >
    >
     
    Stephen Witter, May 28, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian
    Replies:
    1
    Views:
    3,310
    Rick Strahl [MVP]
    Dec 5, 2003
  2. msnews.microsoft.com

    System.IO.FileNotFoundException help

    msnews.microsoft.com, Jun 3, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,264
    msnews.microsoft.com
    Jun 3, 2004
  3. Martinh
    Replies:
    0
    Views:
    2,806
    Martinh
    Aug 15, 2004
  4. =?Utf-8?B?UmFqYQ==?=
    Replies:
    2
    Views:
    847
    bruce barker
    Sep 24, 2004
  5. Stephen Witter

    System.IO.FileNotFoundException using file.copy

    Stephen Witter, May 27, 2004, in forum: ASP .Net Security
    Replies:
    1
    Views:
    209
    She Ven Ng
    Jun 23, 2004
Loading...

Share This Page