C# -Connect unc path with credentials - c#

I have a UNC folder added in my machine using "Add Network Places" option in "My Network Places" (XP).
I need to select the specific unc folder through my C# "folderBrowser Dialogue."
However,as unc path is password protected. While selecting the same,how can I prompt for userCredentials. can anyone have thoughts on this...

PInvoke to WNetAddConnection2 and pass the CONNECT_INTERACTIVE flag to allow the OS to pop a username/password prompt if necessary. You can get the PInvoke definition here.

Related

Directory.CreateDirectory on a network path

I'm trying to create a new directory tree on a network path.
The share is located at \\192.168.5.193\FileContext and has Everyone full access permissions.
This piece of code:
DirectoryInfo directoryInfo = Directory.
CreateDirectory(#"\\192.168.5.193\FileContext\FileContext_Root\General\Test");
gives me:
An exception of type 'System.IO.IOException' occurred in mscorlib.dll
but was not handled in user code
Additional information: Logon failure: unknown user name or bad password.
If I try to open the same address with Windows Explorer, it opens up without password requirements.
The CreateDirectory() documentation states it should accept UNC paths:
You can create a directory on a remote computer, on a share that you have write access to. UNC paths are supported; for example, you can specify the following for path: \2009\Archives\December in Visual Basic, and \\2009\Archives\December in C#.
It also states that IOExceptions could came from:
The directory specified by path is a file .
-or-
The network name is not known.
https://msdn.microsoft.com/en-us/library/vstudio/54a0at6s(v=vs.100).aspx
How can I resolve?
Starting from #JamesThorpe comment:
Everyone isn't Everyone
I came to the solution, just by adding the remote computer to the company domain.
With this action, it's not even necessary to add local computer's NETWORK SERVICE to remote folder's permissions.

Getting permission for accessing an existing file in setup created using Inno Setup

I am a new C# programmer. I made a setup file of an application in Inno setup, but when I use this application after installation, the application crashes when it tries to access (read) an existing folder in the computer (which the user has permission to access otherwise). This folder does not contain any program file, or logs. It just contains some media files which are already in the computer.
I saw the Inno script format, but it shows only how to give permission to access program files/folders only, what about the files which are already there in the computer? Shouldn't the application should have access to files which the user (who installed it)has access to ?
To set permissions on existing files or folders, you can use the Windows cacls command in the [Run] section.
Filename: "{sys}\cacls.exe"; Parameters: """C:\My Folder\My File.ext"" /t /e /g ""Everyone"":f ""Power Users"":f ""Users"":f ""Authenticated Users"":f "; StatusMsg: "Configuring Windows settings..."; Flags: runhidden
Type cacls /? at a command prompt for all available switches and syntax.
It worked when I used
"Permissions: users-modify" in [Dirs] section.

Code Signed .exe on unc path

I have been doing a lot of research on code signing and the windows security prompt. I have a code signing certificate from comodo and everything appears to work great for the most part.
1) If all the files are on my local disk - works fine
2) If the files are on a UNC path and the server is on the same domain as my local machine - gravy
However, If the files are on a UNC path and its on another domain or no domain I get a nasty dialog "We can't verify who created this file..." Even though the file properties dialog shows the file is properly signed with no errors on the certificate.
The command I am using to sign my files
signtool.exe sign /v /ph /tr http://timestamp.comodoca.com/rfc3161 /ac "<comodo key>" /f "<my key>" /p <mypass> <file>
am I missing something or is this just the way it is?

Win32_Process.Create() can launch local files but not network files

I am attempting to remotely call an executable on a target machine, with the executable located on a UNC network path. I am using the Win32_Process.Create method to do this. I am able to use this method to launch files that are stored locally on the C: drive, but I get Return Value 2, Access Denied, when I try to launch the file from a UNC path. I am confident the path is correct, because if I alter it to a bogus path, I get Return Value 9, Path Not Found.
In Powershell I am using invoke-wmimethod to call the Create method of Win32_Process and passing a credential object that has administrative rights on the target system and read rights on the UNC path. In C# I am impersonating using ConnectionOptions with the same credentials. The results are the same in both cases.
I have also tried using various methods (CIM_DataFile, remotely invoking XCopy) to copy the EXE file locally first. None of these methods have worked. I want to copy directly from a file server, to a target system, without pulling the file to the application server first, because the application server is not in the same datacenter as most target systems and as such would be pulling a large file down over the WAN twice, which is slow and less reliable. One option I have found is to use FTP, but I consider that a last resort.
I can also remotely invoke the EXE from the UNC path using PSExec and the same credentials, but I want to avoid shelling out from my web application to call PSExec. I know it will work if that's what I have to do, and I have used PSExec many times to solve problems like this, but I really want to do this all within the application and not hacking around it using an external program.
Is there any way I can use Win32_Process to launch an EXE on a remote machine, when that EXE is located on a UNC path? Could this be a Group Policy issue wherein the process launched by WMI does not have permission to invoke an EXE from a network location? I am out of ideas and out of search terms.
Powershell code examples. This works:
$launchproc = Invoke-WmiMethod -ComputerName $compName -Class Win32_process -Name Create -ArgumentList "c:\temp\installer.exe /s /f1c:\temp\installer.iss" #-Credential $adminCreds
This does not:
$launchproc = Invoke-WmiMethod -ComputerName $compName -Class Win32_process -Name Create -ArgumentList "\\fileserver\share\installer.exe /s /f1\\fileserver\share\installer.iss" -Credential $adminCreds
Note that if I issue the command locally from a command window, interactively, the UNC based command DOES work just fine. The funny syntax is an artifact of InstallShield's silent install switches. Also note that if I double-backslash or backtick escape the backslashes, I get Path Not Found, so I don't think it's an escaping issue.
Edit: while not exactly the same problem, I did check the GP rights described here: WMI Win32_Process.Create fails with Insufficient Privs and I do have those rights set correctly.
Edit #2: I found someone else having a similar problem:
Win32_Process Create method. Trying to copy a file from a remote machine to a remote machine Again it's a batch file-ish hack launching Net Use on the remote system. Is this my only real option?
In the end, I just called PsExec using System.Diagnostics.Process() in C#. It's not the solution I wanted, but I wasn't able to make anything else work, and while I don't like shelling out to an external EXE, it does end up being relatively straightforward.
I asked some of the PowerShell MVPs who are more knowledgeable in WMI and got the response that unless the remote machine was the domain controller you won't be able to do it. However, if you could use PowerShell remoting to remote to the computer with -Authentication CredSSP, then you could use Invoke-WmiMethod with the network path. Richard Siddaway did a write up on this that might be useful to peruse.

Access to the path is denied when programmatically downloading a file from ftp in C# on Win7 64bit

I'm using the C# framework REBEX to download file(s) from an FTP on Win7 64bit in VS2008.
After I press F5 to start debugging, I get an error that Access to the path I'm downloading to is denied.
I believe this may be due to UAC and elevated permissions issues on Win7/Vista. I did some research and found some information on app.manifest, but when I change the requestedExecutionLevel in the app.manifest, it mentions that it isn't compatible with ClickOnce Security Settings. So I disabled ClickOnce only to have it automatically re-enable itself when building the project or debugging. And yes, I've enabled full security privileges for all users on the folder, and yes I've tried sending the files to my ...\AppData\Roaming\
Stupid me. Turns out I wasn't setting the full local filepath. Just to help anyone else in my predicament...
This will create an access denied exception:
client.GetFile(item.Name, #"C:\DIR\");
And this will get you the file you want:
client.GetFile(item.Name, #"C:\DIR\" + item.Name);

Categories

Resources