Able to write file but not read from a network location - vb.net

I have following network location
Dim myfolder As String = "\\10.0.0.90\myfolder\"
I am able to create a new file in this folder using following code:
File.Create (myfolder)
But when I try to read contents of this folder using code below I get error.
Code
Dim orderedFiles = New System.IO.DirectoryInfo(myfolder).GetFiles()
Error
The system detected a possible attempt to compromise security. Please
ensure that you can contact the server that authenticated you.
File writing is being done by ASP.Net page while reading is done from Windows Service. Could this be the issue?

Windows Service was running as "Local System". I right click on it, went into properties and changed the "Log on as" to some user account and now it can access network folder.

Related

Using Redemption with Outlook 2016 - "Could not find the store DN" with GetSharedMailbox

I had piece of code that was working perfectly until earlier this week, when I changed my domain password. After that, when trying to open a shared mailbox using GetSharedMailbox, I receive the error Could not find the store DN. Now, the really weird thing is that this only happens on one machine (unfortunately, the machine I really need it to run on). It works fine on my development laptop. Both machines have been rebooted, and logged back in repeatedly since then.
Both machines are logged into a Windows AD domain, under my account, and have no problems accessing other domain resources. I can open Outlook on the problem machine with no problem, and view both my personal inbox and the shared mailbox. Remote debug on the problem machine shows that it's opening a connection to Exchange ok (rdo.Logon works, and I can walk through its objects). The error is thrown when I try to open the shared mailbox.
Any suggestions would be welcome!
Try
Dim rdo as RDOSession = New RDOSession
rdo.Logon()
Dim sharedBox As RDOStore = rdo.GetSharedMailbox("SharedMailbox#domain.com") 'Throws error here
inbox = sharedBox.GetDefaultFolder(rdoDefaultFolders.olFolderInbox)
Console.WriteLine(String.Format("Mailbox: {0} ({1})", sharedBox.Name, sharedBox.EntryID))
' Do a bunch of stuff with inbox.Items
Catch err As Exception
My.Application.Log.WriteException(err, TraceEventType.Error, "Error connecting to mail server")
End Try
The error usually means that Redemption does not have the credentials to retrieve autodiscover XML and configure the connection to the delegate mailbox. If you were using LogonHostedExchangeMailbox instead of Logon, Redemption would have used the specified credentials. Otherwise you can cache the credentials using RDOSession.Creadentials.Add.

Opening a file from a web app VB.NET

I am currently trying to open a file on my web app by retrieving the file path from a sql database
here is my code that will open the file when I double click an index in a listbox.
Protected Sub userdoubleClick()
Dim selectedPath As String
Try
fileConnection.Open()
selectedFile = FileListBox.SelectedValue
'takes the selected items from the listbox and searches the database for the file
'and will open the file for the user
fileCommand.CommandText = "SELECT filePath FROM ImportedFiles WHERE FileName = '" & selectedFile & "'"
fileDataReader = fileCommand.ExecuteReader
Do While fileDataReader.Read
selectedPath = fileDataReader.GetValue(0)
Loop
System.Diagnostics.Process.Start(selectedPath)
Catch ex As Exception
End Try
fileConnection.Close()
End Sub
When I run this on my local PC it works fine but then when I publish it to a server it wont open the file.
System.Diagnostics.Process.Start(selectedPath) will open the file on the machine running the web application; in other words, it is opening it on your local machine when running locally, but opening on the server when deployed. This just isn't going to work.
See ASP.Net Download file to client browser
If you're accessing the files from a web application, then the user identity under which the file is opened is not that of the user logged into your application. It's the user under which the IIS application pool is running. That's the user that needs to have permission granted to your shared drive.
There are many solutions to this problem, including changing the context under which the application pool is running or making the files local.

Word Automation : could not open macro storage

My application (vb.net windows application deployed via ClickOnce) uses Word to open and fill .dot templates to create new Word documents. I reference Microsoft Word 14 Object Library and uses this code :
Dim oWord As Word.Application = Nothing
Dim oDoc As Word.Document = Nothing
Try
oWord = New Word.Application
Dim strFileName As String = ""
Select Case strType
Case "LettreReception"
strFileName = Path.Combine(GetParam(1), "Template_LettreReception.dot")
If File.Exists(strFileName) Then
oDoc = oWord.Documents.Add(strFileName)
On the last line I receive "could not open macro storage" error on deployed machines (not on my development machine).
I develop with Windows 7 - Office 2010 - VS 2010 (.Net 3.5). My deployment machine is also a Windows 7 with Office 2010 installed.
I tried to remove normal.dotm (I found some links advicing it) without success. The .dot template used contains no macro.
Check the properties of the word document and make sure the files are unblocked. Sometimes when you get the documents from a different computer or download them from the internet they will be blocked which will cause the the throwing of this exception "could not open macro storage"
Because Word Interop is actually running behind the scenes as if it was running in an interactive session, certain permissions are required of the account used during execution.
Are you using Windows Authentication and impersonation in you web app? If so, the user being impersonated must have local log on rights to the server to run Word... In addition, you must actually log on to the server with that account at least once so that a profile exists on that machine for that user so the registry hive can be loaded. I've also found that you may need to actually run Word at least once as that user (to make sure any first-time initialization messages get taken care of before trying to run Word from code).
If not, then the service account that the application is running under (usually NETWORK SERVICE) requires the aforementioned permissions (which I will describe shortly) and you'll have to do something fancy like loading a registry hive dynamically at run-time. Personally, I prefer implementing an in-code temporary impersonation with a user account that has local log on permissions on the server in question.
Local log on permissions can be a bit tricky depending on your network and group policy configurations (if you want to be somewhat secure and not just use a Domain Admin account).
The reason everything works on your computer running in VS is because the context of the web application is YOUR user account - which, of course, has local log on permissions on your machine with a registry hive that can be loaded.
Now for the permissions:
First, you must run "dcomcnfg" on the server and make the following configuration change:
Right click on Component Services\computers\My Computer\DCOM Config\Microsoft Word 97 - 2003 Document and go to Properties
In the Properties screen, go to the Security tab and change the "Launch and Activation Permissions" to Customize.
Click the Edit button and add the local computer NETWORK SERVICE account (If not using impersonation... If using impersonation, add the appropriate user or group) to the list of users and check "Local Launch" and "Local Activation"
Make sure that the local computer NETWORK SERVICE account (If not using impersonation... If using impersonation, then the appropriate user or group) has appropriate read/modify permissions on the folder(s)and file(s) that you will be opening and/or saving to.
Create a "Desktop" directory under: C:\Windows\System32\config\systemprofile\ and give Full permissions to the local NETWORK SERVICE account (or the account that your ASP .NET application is running under) [NOTE: I believe this and the next step only apply if NOT using impersonation]
Give Modify/Read/Execute permissions on the C:\Windows\System32\config\systemprofile folder
Hope that helps some and wasn't too confusing...
Right click on the file that is opened -> Click the Unblock tick box -> Apply
Worked for me at least.
"could not open macro storage" is telling you that VBA is looking for a particular structured storage file such as a .DOT or .DOC, and looking for the storage (a kind of stream within the file) in that file that contains the VBA code. If it can't open it, possible reasons include:
the container (the .doc/.dot) isn't there
the container cannot be opened with the caller's permissions
the container is there but the storage isn't there (e.g. on the target system there is a container with the expected name, but it contains no macros)
the container is there and the storage is there but cannot be opened with the caller's permissions
So one thing to do is to look through your project looking for anything it references (perhaps even other objects or DLLs that you specified via Tools->References) that is not also being delivered with your template.
Go to the Word document (if it's a template, be sure to open it, not create a new document with it) and disable Protected View:
https://casecomplete.zendesk.com/hc/en-us/articles/200685047-Could-not-open-macro-storage

Drive Letters Usage with the FileSystemObject - VBScript

I am migrating from a Win 2003 server to a Win 2008 R2 server. I'm running a classic ASP application. In the 2003 server the following code worked for creating a file:
CONST SCRATCH_DIR = "E:\Temp\"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set TFL = FSO.CreateTextFile(SCRATCH_DIR & "Debug.txt", True)
On the NEW server the Set TFL line gives me this error:
Microsoft VBScript runtime error '800a0046'
Permission denied
/inc/vbutil.inc, line 110
If I remove the Drive designation "E:\" from the CONST, the procedure works just fine; (except it writes to the C:\ drive) so I'm sure the error message is correct.
Could anyone please point me at where/how I'd change permissions for this (E:\Temp) directory?
Thanks very much in advance,
(A dumb application programmer;)
You can change permissions on the folder by right clicking it > Properties > Security > Advanced.
Uncheck "Include inheritable permissions from this object's parent".
Click "Add"
Modify permissions for the user running the script. They will need read, write and create permissions.

FileUpload SaveAs UnauthorizedAccessException error (Dotnetnuke)

I am trying to save an image file in a custom module I am building for a DNN site.
However when I run the code I get an UnauthorizedAccessException.
if(upLoadAddImg.HasFile)
{
String imageLocation = ConfigurationManager.AppSettings["ImageFolderPath"];
//Upload file
upLoadAddImg.SaveAs(Server.MapPath(imageLocation));
}
I am running on localhost using the internal visual studio server. Tthe folderpath is all right and I have made sure Network Service has full permissions.
Am I missing something obvious or does DNN have some special permission setting I am missing?
If you are using the dev server in VS its identity is not Network Service by default. Most likely the directory you are saving to loccally is not allowed for the aspnet user - if you run this on a web site it should work at least code wise -------
To test it you can do one of two things make the portal directory open to everyone or set up a local site not run on the dev server ----