SFTP download with .NET CORE 3.1 - c#

Is there a way to download a file from a sftp server with .net core 3.1 ?
I found various answers which are all outdated. (for example, ssh.net cannot be found anymore)
How is the current status in this topic?

There is an updated version of SSH.net called Neon.SSh.Net
https://www.nuget.org/packages/Neon.SSH.NET/
works in the same way as the old version from Renci.
...
var client = new SftpClient(host,port,username,password);
client.Connect();
using (Stream fileStream = File.OpenWrite("c:\\temp\\file.tmp"))
{
_client.DownloadFile(ftpFilePath, fileStream);
}
...

Related

.NET Core 2.2 Receiving 401 Unauthorized response when calling IIS site on same server with Windows Auth

I'm having some strange behavior in my .NET Core 2.2 application.
When trying to make an HTTP request (through either HttpClient or RestSharp), as long as the site that I am requesting is hosted on a different server than where my calling application resides, I receive a 200 response, just like I'd expect, which is great.
However, as soon as I try to hit a site on the same server (and I've tried this with the exact same site hosted on both servers), I get a 401 Unauthorized.
For reference, the site that I am requesting is a ASP.NET Web API on .Net Framework, and it uses Windows Auth.
I've tried this in .Net Framework 4.6.2 and .Net Core 3.0, and both of them work fine, and do not exhibit this problem -- it seems just to affect 2.2 for me.
I know there is the "Loopback Check Issue", however if this was the case, I'd expect .Net Framework and .Net Core 3.0 to face the same problem.
I also am aware of This GitHub Issue however that seems to have been solved in 2.2.
For reference here is some sample code that demonstrates the problem with HttpClient (The same codes is used for all versions of .Net)
static async Task Main(string[] args)
{
await MakeRequest();
}
public async static Task MakeRequest()
{
Console.WriteLine("Enter URL:");
var url = Console.ReadLine();
var uri = new Uri(url);
try
{
var client = new HttpClient(new HttpClientHandler { UseDefaultCredentials = true });
var res = await client.GetAsync(uri);
res.EnsureSuccessStatusCode();
var content = await res.Content.ReadAsStringAsync();
Console.WriteLine(res.StatusCode);
Console.WriteLine(content);
}
catch (Exception e)
{
Console.WriteLine($"Error: {e}");
}
finally
{
await MakeRequest();
}
}
Any ideas?
EDIT:
SDK used: 2.2.104
Runtime used: 2.2.5
2.2.5 seems to be the version of .NET core runtime version instead of .NET CORE SDK version.
The latest version of 2.2 sdk is 2.2.4.
Since 2.2.7 would be the update verion of 2.2.5 and this issue is not happening in runtime 2.2.7. Is it acceptable to just update runtime 2.2.7 on your server?

Issue downloading files with WebClient

Tested WebClient on both project type (WPF,Windows form application)
With the below code:
String[] txt = { "someURLstring", "otherURLstring" };
foreach (String str in txt)
{
using (WebClient wc = new WebClient())
{
wc.DownloadProgressChanged += wc_DownloadProgressChanged;
wc.DownloadFileCompleted += wc_DownloadFileCompleted;
await wc.DownloadFileTaskAsync(new Uri(str), "somename.rar");
}
}
Both projects are developed using Visual Studio 2015 with .net framework 4.5 on windows 10.
The issue is that, when those applications are not running on the PC that they are developed, the files that are downloaded are 95 KB each, but they are 2.5 MB each.
I tried to build them using .net framework 4.6 but still same issue.
Any ideas?
Both are tested on 2 other PC's using windows 7/10 with .net framework 4.6
EDIT:
I changed links to download files from my own webserver (file are .zip) and everything worked fine. Previous links though work only on my PC.
Any ideas?
Ok, well my bad I didn't mention that the first links that were used come from mediafire. Though they are using captcha system that's why other users can't download the files.

Access remote file contents as a stream using WinSCP .NET assembly

I am trying to open file to read from SFTP using WinSCP .NET assembly as par to my exercise to archive file from SFTP to Azure blob.
To upload a blob to Azure, I am using
using (var fileStream = inputStream)
{
blockBlob.UploadFromStream(fileStream);
blobUri = blockBlob.Uri.ToString();
}
How to get the stream from the file on SFTP server?
I managed using SftpClient to get the stream using the following code and it works but unfortunately not able to achieve the same using WinSCP .NET assembly.
sftpClient.OpenRead(file.FullName)
Can anyone help me how to achieve the same using WinSCP .NET assembly?
Because I need to connect to SFTP using username, password and privatekey I am using WinSCP .NET assembly.
Thanks
The WinSCP .NET assembly supports providing the contents of a remote file using streams using the Session.GetFile method:
using (Stream stream = session.GetFile("/path/file.ext"))
{
blockBlob.UploadFromStream(stream);
}

how to get connect with ibm websphere mq by using c#.net

can any one guide me on, to get connect with ibm websphere mq by using c#.net, reason was i am trying to push the message in to MQ, kindly can any give me suggestion to connect by using c#.net
There is an IBM supplied dll (since v5.3 Fixpack8) on Windows called amqmdnet.dll, which is a .NET assembly providing an IBM supported model for MQSeries. (Reference) It is usually located in C:\Program Files\IBM\WebSphere MQ\bin\amqmdnet.dll
If you need more direction, there are several examples on how to communicate with MQ from .NET on CodeProject:
http://www.codeproject.com/Articles/12198/IBM-WebSphere-MQ-with-C-GUI-application-that-is-bo
http://www.codeproject.com/Articles/37807/How-to-Setup-a-Websphere-MQ-with-C-NET-GUI-to-Put
http://www.codeproject.com/Articles/6212/C-and-WebSphere-MQ-formerly-MQSeries-Client-Server
Also, there's this walkthrough that could be helpful: http://www.c-sharpcorner.com/UploadFile/pk_khuman/AquickstartCsharpWebsphereMQ07112006024017AM/AquickstartCsharpWebsphereMQ.aspx
You can connect using the .NET libraries provided by IBM; however, they require you to install the WebSphere MQ Client on every server you deploy your solution to. (lame)
If using WebSphere MQ, the machine used to run the XMS application
must be installed with the WebSphere MQ Client V7.0.1.0 or later
You can avoid this by converting a few Java libraries using IKVM (www.ikvm.net).
The whole process should only take about 15 minutes.
You'll still need to download and install the client on your development box so that you can get the JAR files. After you convert them, you can uninstall the client.
Here are the steps
1) Get JARs
Download WebSphere MQ V7.5 Client: http://www-304.ibm.com/support/docview.wss?uid=swg24032744
Install the MQ client: You only need to install the "Java and .Net Messaging and Web Services".
2) Convert JARs
Download IKVM: www.ikvm.net
Extract the IKVM files (e.g. c:\tools\IKVM).
Open Win command prompt
Execute command: set path=%path%;c:\tools\IKVM\bin
Execute command: cd C:\Program Files (x86)\IBM\WebSphere MQ\java\lib
Execute command: ikvmc -target:library -sharedclassloader { com.ibm.mq.jmqi.jar } { com.ibm.mqjms.jar } { dhbcore.jar } { jms.jar }
3) Copy JARs
Open windows explorer.
Navigate to: C:\Program Files (x86)\IBM\WebSphere MQ\java\lib
Copy the following files:
**com.ibm.mq.jmqi.dll
com.ibm.mqjms.dll
jms.dll**
Navigate to: c:\tools\IKVM\bin
Copy the following files:
**IKVM.Runtime.dll
IKVM.OpenJDK.Core.dll**
Move the copied files to a 3rd Party folder in your project/solution.
4) References JARs
Reference the copied JARs. Please note that you can skip the previous Copy JARs step above and simply reference the libraries directly, if you like. The objective was to show that there were no other resources needed for proper execution.
The following is a very simple example of how you can use the libraries.
using com.ibm.msg.client.jms;
using com.ibm.msg.client.wmq.common;
using javax.jms;
using System;
class Program
{
static void Main(string[] args)
{
var ff = JmsFactoryFactory.getInstance(JmsConstants.__Fields.WMQ_PROVIDER);
var cf = ff.createConnectionFactory() as JmsConnectionFactory;
cf.setIntProperty(CommonConstants.__Fields.WMQ_CONNECTION_MODE, CommonConstants.__Fields.WMQ_CM_CLIENT);
cf.setStringProperty(CommonConstants.__Fields.WMQ_HOST_NAME, "<YOUR INFO>");
cf.setIntProperty(CommonConstants.__Fields.WMQ_PORT, 1414);
cf.setStringProperty(CommonConstants.__Fields.WMQ_CHANNEL, "<YOUR INFO>");
cf.setStringProperty(CommonConstants.__Fields.WMQ_QUEUE_MANAGER, "<YOUR INFO>");
var connection = cf.createConnection();
var session = connection.createSession(false, Session.__Fields.AUTO_ACKNOWLEDGE);
var queue = session.createQueue("queue:///<YOUR INFO>");
var producer = session.createProducer(queue);
var msg = session.createTextMessage();
msg.setStringProperty("JMSXGroupID", Guid.NewGuid().ToString());
msg.setIntProperty("JMSXGroupSeq", 1);
msg.setBooleanProperty("JMS_IBM_Last_Msg_In_Group", true);
msg.setText("Hello World");
connection.start();
producer.send(msg);
producer.close();
session.close();
connection.close();
}
}
There are number of samples that come with MQ product install. Refer Nmqsput.cs for your case. When creating a new project you will need to add amqmdnet.dll as reference.
Not sure what version of MQ you are using. I am assuming you are using MQ v701. You can find the samples under tools folder of your MQ installation.
If are looking for JMS style of messaging in C#, then XMS .NET is worth looking at. You can find the samples of XMS .NET in the same folder as MQ samples. XMS .NET reference is here
Already converted (java to c#) mq-client -
nuget,
github
c# example:
var ff = JmsFactoryFactory.getInstance(JmsConstants.__Fields.WMQ_PROVIDER);
var cf = ff.createConnectionFactory();
cf.setIntProperty(CommonConstants.__Fields.WMQ_CONNECTION_MODE, CommonConstants.__Fields.WMQ_CM_CLIENT);
cf.setStringProperty(CommonConstants.__Fields.WMQ_HOST_NAME, "127.0.0.1");
cf.setIntProperty(CommonConstants.__Fields.WMQ_PORT, 8010);
cf.setStringProperty(CommonConstants.__Fields.WMQ_CHANNEL, "EXAMPLE.CHANNEL.ONE");
cf.setStringProperty(CommonConstants.__Fields.WMQ_QUEUE_MANAGER, "EXAMPLE_QUEUE_MANAGER");
cf.setStringProperty(CommonConstants.__Fields.WMQ_APPLICATIONNAME, "JMS EXAMPLE");
cf.setStringProperty(CommonConstants.USERID, "EXAMPLE_USER");
using (var context = cf.createContext())
{
var queue = context.createQueue("queue:///EXAMPLE_QUEUE_NAME");
var producer = context.createProducer();
producer.send(queue, "Hello World");
}

FTPWebRequest .NET 3.5 vs 4

I have an application that does an FTP connection to an IBM mainframe and all has been working fine. I then recompiled against .NET 4 and the mainframe no longer accepts the file giving me an error message stating either the file doesn't exist, or I don't have permission for it.
This is what I use to send the file:
Ftp = (FtpWebRequest)FtpWebRequest.Create(FtpAddress + "%2F'" + "EM.MOM.FIMSDATA" + ".AA." + "RR(+1)" + "'");
Has Microsoft changed anything between .NET 3.5 and 4 because this points to a framework change?
I found this on Microsoft.
The cause of this issue is due to a behavior change in the
System.Net.FtpWebRequest class in .Net Framework 4. There has been a
change made to the System.Net.FtpWebRequest class from .Net Framework
3.5 to .Net Framework 4 to streamline the use of the CWD protocol commands. The new implementation of the System.Net.FtpWebRequest class
prevents the send of extra CWD commands before issuing the actual
command which the user requested and instead directly sends the
requested command. For fully RFC compliant FTP servers, this should
not be an issue, however for non-fully RFC compliant servers, you will
see these types of errors.
Solution is on:
http://support.microsoft.com/kb/2134299

Categories

Resources