Wednesday, March 28, 2012

System.IO.Directoryinfo throwing exception

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 TryAnother 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/d...>
NetHT05.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@.discussions.microsoft.com> wrote in message
news:9B4BC708-162A-4C50-8AF1-85D829D5E579@.microsoft.com...
>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
>
Problem solved! All I had to do was set "identity impersonate" to "true" in
my web.config. Thank you so much!
"John Timney (ASP.NET MVP)" wrote:

> 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 t
o
> 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/d...
ecNetHT05.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 us
e
> 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@.discussions.microsoft.com> wrote in message
> news:9B4BC708-162A-4C50-8AF1-85D829D5E579@.microsoft.com...
>
>
glad your sorted :)
Regards
John Timney
ASP.NET MVP
Microsoft Regional Director
"Glenn Venzke" <GlennVenzke@.discussions.microsoft.com> wrote in message
news:CCC31167-8012-4FC7-9EC7-3F125F515339@.microsoft.com...
> Problem solved! All I had to do was set "identity impersonate" to "true"
> in
> my web.config. Thank you so much!
> "John Timney (ASP.NET MVP)" wrote:
>

0 comments:

Post a Comment