Re: System.IO.Directoryinfo throwing exception



Another MVP has written a great article about this
http://west-wind.com/weblog/posts/1572.aspx

The crux is (and its a pain to get working) that using Anonymous
authentication (so no impersonation) the local ASPNET account has to have
the same credentials (username and password) on both machines in order to
delegate security. With basic authentication and impersonation you need to
use a domain account which can delegate and you can check how to mark your
impersonated account able to use security delegation here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT05.asp.
This is also required if you want to use NTLM authentication over kerberos

Personally, I would implement a the remote server as an http device and use
http requests, checking the referrer whihc makes life a lot simpler than
using mapped network drives.
--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

"Glenn Venzke" <GlennVenzke@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:9B4BC708-162A-4C50-8AF1-85D829D5E579@xxxxxxxxxxxxxxxx
>I have an aspx page that is set up to copy backed-up DB files from a shared
> directory to a local folder. For some reason, it is being denied access to
> the network share. I have the web app running under a domain account that
> I
> know for a fact has access. it works fine when I log on to the network and
> browse the directory manually. Even when I grant full control to everyone
> on
> the share and the underlying folder itself, I still can't access it
> through
> code. The funny thing is though, this same code works fine when executed
> within a windows service which runs under the same domain account. Help!
>
> Code follows:
>
> **** this first line is the one throwing the exception ********
> Dim objFromDirectory As DirectoryInfo = New
> DirectoryInfo("\\[server]\[network share]
>
>
> Dim strNewBackupPath As String = "C:\SQLBackUp\Data"
> Dim objToDirectory As DirectoryInfo = New
> DirectoryInfo(strNewBackupPath)
> Dim objOldBackupFiles() As FileInfo =
> objToDirectory.GetFiles()
> Dim objNewBackupFiles() As FileInfo =
> objFromDirectory.GetFiles()
> Dim x As Integer
> Dim intNewBackupFileCount As Integer =
> UBound(objNewBackupFiles)
> Dim objCurrentFile As FileInfo
> Dim objBackUpLog As XmlDocument = New XmlDocument
> Dim objNewParentNode As XmlElement
> Dim objRootNode As XmlElement
> Dim objLogEntryDateNode As XmlElement
> Dim objNewChildNode As XmlElement
> Dim objDateNodeList As XmlNodeList
> Dim strStartTime As String
> Dim strEndTime As String
> Dim intFileSize As Integer = 0
> Dim intLogEntryCount As Integer
> Dim strBackUpLogPath As String =
> "C:\SQLBackUp\SQLBackupLog.xml"
> Dim objCurrentDateNode As XmlElement
> Try
> strStartTime = DateTime.Now.ToLongTimeString()
>
> '-- delete old set of backups. GV 1/28/05
> If UBound(objOldBackupFiles) > 0 Then
> For x = 0 To UBound(objOldBackupFiles)
> File.Delete(objOldBackupFiles(x).FullName)
> Next
> End If
>
> '-- copy new set of backups. GV 1/28/05
>
> '- if no backup files are found, throw an exception. GV
> 6/2/05
> If intNewBackupFileCount < 1 Then
> Throw New ApplicationException("No files were
> found.")
>
> Else
> For x = 0 To intNewBackupFileCount
> objCurrentFile = objNewBackupFiles(x)
> intFileSize = intFileSize +
> objCurrentFile.Length
> objCurrentFile.CopyTo(strNewBackupPath & "\" &
> objCurrentFile.Name)
> Next
> End If
>
> strEndTime = DateTime.Now.ToLongTimeString()
>
> '-- write file-copy info to XML log. GV 1/28/05
> objBackUpLog.Load(strBackUpLogPath)
> objNewParentNode =
> objBackUpLog.CreateElement("logentry")
>
> objBackUpLog.DocumentElement.AppendChild(objNewParentNode)
>
> ' current date
> objNewChildNode = objBackUpLog.CreateElement("date")
> objNewChildNode.InnerText =
> DateTime.Now.ToShortDateString()
> objNewParentNode.AppendChild(objNewChildNode)
>
> ' time started
> objNewChildNode =
> objBackUpLog.CreateElement("starttime")
> objNewChildNode.InnerText = strStartTime
> objNewParentNode.AppendChild(objNewChildNode)
>
> ' time ended
> objNewChildNode = objBackUpLog.CreateElement("endtime")
> objNewChildNode.InnerText = strEndTime
> objNewParentNode.AppendChild(objNewChildNode)
>
> ' total amount copied (in megs)
> objNewChildNode =
> objBackUpLog.CreateElement("dataamount")
> objNewChildNode.InnerText = CStr(Math.Round(intFileSize
> / 1048576, 2) & " Mb")
> objNewParentNode.AppendChild(objNewChildNode)
>
> ' total number of files copied
> objNewChildNode =
> objBackUpLog.CreateElement("nbroffiles")
> objNewChildNode.InnerText = CStr(intNewBackupFileCount
> +
> 1)
> objNewParentNode.AppendChild(objNewChildNode)
>
> '--- delete entries older than 4 months. GV 1/31/05
> objDateNodeList =
> objBackUpLog.SelectNodes("/log/logentry/date")
> For Each objCurrentDateNode In objDateNodeList
> If DateValue(CDate(objCurrentDateNode.InnerXml)) <
> DateValue(DateAdd("d", -120, DateTime.Now)) Then
> objCurrentDateNode.RemoveAll()
> objRootNode =
> objCurrentDateNode.ParentNode.ParentNode
>
> objRootNode.RemoveChild(objCurrentDateNode.ParentNode)
> End If
> Next
>
> '--- save changes. GV 1/31/05
> objBackUpLog.Save(strBackUpLogPath)
> intOperationSuccess = 1
> Catch exc As Exception
> '--- if exception occurs, write error info to XML log.
> GV 1/28/05
> objBackUpLog.Load(strBackUpLogPath)
> objNewParentNode =
> objBackUpLog.CreateElement("logentry")
>
> objBackUpLog.DocumentElement.AppendChild(objNewParentNode)
>
> ' current date
> objNewChildNode = objBackUpLog.CreateElement("date")
> objNewChildNode.InnerText =
> DateTime.Now.ToShortDateString()
> objNewParentNode.AppendChild(objNewChildNode)
>
> ' error header
> objNewChildNode =
> objBackUpLog.CreateElement("errorheader")
> objNewChildNode.InnerText = "** ERROR **"
> objNewParentNode.AppendChild(objNewChildNode)
>
> ' error description
> objNewChildNode =
> objBackUpLog.CreateElement("errordescription")
> objNewChildNode.InnerText = exc.Message.ToString
> objNewParentNode.AppendChild(objNewChildNode)
> objBackUpLog.Save(strBackUpLogPath)
> objEventLog.WriteEntry("**** ERROR **** " &
> exc.Message.ToString() & " occurred on " & DateTime.Now & ".")
> intOperationSuccess = 0
> '---
> Finally
> '--- destroy all objects. GV 1/28/05
> x = Nothing
> intFileSize = Nothing
> intLogEntryCount = Nothing
> If Not (objBackUpLog Is Nothing) Then objBackUpLog =
> Nothing
> If Not (objCurrentFile Is Nothing) Then objCurrentFile
> =
> Nothing
> intNewBackupFileCount = Nothing
> If Not (objNewBackupFiles Is Nothing) Then
> objNewBackupFiles = Nothing
> If Not (objOldBackupFiles Is Nothing) Then
> objOldBackupFiles = Nothing
> If Not (objToDirectory Is Nothing) Then objToDirectory
> =
> Nothing
> If Not (strNewBackupPath Is Nothing) Then
> strNewBackupPath = Nothing
> If Not (objFromDirectory Is Nothing) Then
> objFromDirectory = Nothing
> If Not (strStartTime Is Nothing) Then strStartTime =
> Nothing
> If Not (strEndTime Is Nothing) Then strEndTime =
> Nothing
> If Not (objRootNode Is Nothing) Then objRootNode =
> Nothing
> If Not (objNewParentNode Is Nothing) Then
> objNewParentNode = Nothing
> If Not (objNewChildNode Is Nothing) Then
> objNewChildNode
> = Nothing
> If Not (objDateNodeList Is Nothing) Then
> objDateNodeList
> = Nothing
> If Not (objCurrentDateNode Is Nothing) Then
> objCurrentDateNode = Nothing
> If Not (objLogEntryDateNode Is Nothing) Then
> objLogEntryDateNode = Nothing
> If Not (strBackUpLogPath Is Nothing) Then
> strBackUpLogPath = Nothing
> '---
> End Try
>


.



Relevant Pages

  • Re: Cant make a domain user the "anonymous access" user
    ... When dealing with authentication issues it is VERY important to ... Some of the things you claim is not consistent with a default IIS ... If you use a browser that cannot do NTLM, by definition, a 401.2 error is ... user account that works and your domain account that does not. ...
    (microsoft.public.inetserver.iis.security)
  • Re: How to use WindowsPrincipal properly??
    ... the administrators in the computer e.g. win2k ... I am basically confused with the role base authentication and the ... impersonation, not sure what exactly is the difference. ... > string ONLY returns the string "Administrator", ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Access denied ( From one site to another, that is in another server)
    ... server. ... you can implement impersonation through code and revert ... This posting is provided "AS IS", with no warranties, and confers no rights. ... | Integrated Authentication ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Cant make a domain user the "anonymous access" user
    ... I have tried both with and without Integrated authentication enabled. ... I get a login prompt if I am using an NTLM-capable ... I can then authenticate using the domain account ... Pool containing this ASP page ...
    (microsoft.public.inetserver.iis.security)
  • Re: localhost vs. macinename in URL (access denied)
    ... Impersonation with Integrated Authentication will work if you are accessing ... a resource on the same machine. ... being delegated to allow delegation or change the computer account to allow ...
    (microsoft.public.dotnet.security)