How to download file using web service in C#? - c#

How to download a file via web service ? And how the client application accept this?
I write down the code as below in the client app, it throws an exception "Access Denied"
wsDownload.wsDownloadFile downFile = new wsDownload.wsDownloadFile();
byte[] file = downFile.DownloadFile(strFileName, "", "", "");
MemoryStream mStream = new MemoryStream(file);
any response is appreciated.

"Access denied" probably just means the local web service user doesn't have read access to the local copy of the file.
Ron

It's probably a matter of ntfs permissions on the service side. You should grant read permissions on the folder that contains the file you are downloading to the user account under which is running the WS, usually IIS_WPG.

Related

Copy File to remote windows server using .net core

I have put endpoint that accept the zip file. The endpoint is working fine to get the zip file. Now from that endpoint after I get that file I am trying to copy that file to a different remote Server.
I try below code to connect to remote Server using the below code by referring to micrsoft docs https://learn.microsoft.com/en-us/dotnet/api/system.security.principal.windowsidentity.runimpersonated?view=net-6.0:
bool returnValue = LogonUser(#"ACC\Test.test", "acc.local", "password",
9, 0,
out safeAccessTokenHandle);
if (returnValue)
{
WindowsIdentity.RunImpersonated(safeAccessTokenHandle, () =>
{
//Destination remote server folder to upload file
var pathToUploadFile = #"\\152.158.100.45\D\FileUpload";
string fileName;
fileName = file.FileName;
var path = Path.Combine(pathToUploadFile, fileName);
using (var stream = new FileStream(path, FileMode.Create))
{
//save a zip file to folder
file.CopyTo(stream);
}
});
}
In this code I get error of username and password is incorrect in this line of code:
var stream = new FileStream(path, FileMode.Create)
I am not even sure if the LogonUser method connects to a remote server. So is there is better way to do this copy file to remote server Please give an idea to implement in .net core 6.0 C#. Any help is really appreciated. Thanks
Finally I am able to solve the issue by implementing the Win32 API called WNetUseConnection solutions that was mentioned on below questions:
Accessing a Shared File (UNC) From a Remote, Non-Trusted Domain With Credentials

Unwanted files cannot be removed with SetAccessControl

We have an application on a shared server, and some unwanted files appeared on our ftp (asp file, certainly by injection or lack of security). The application is in C#.
Impossible to remove those files by FTP (unauthorized).
I've made a GetOwner request and those files seem to have as owner DOMAIN\IISUSR.
I try to change owner or to add ACL on the file, but I receive an unauthorized exception.
System.IO.FileInfo sFile = new System.IO.FileInfo(sPathFile);
System.Security.AccessControl.FileSecurity sFS = sFile.GetAccessControl();
if (System.IO.File.Exists(sPathFile))
{
try
{
IdentityReference sGet = new NTAccount(#"DOMAIN\MYUSER");
sFS.AddAccessRule(new FileSystemAccessRule(#"DOMAIN\IISUSR", FileSystemRights.FullControl, AccessControlType.Allow));
sFile.SetAccessControl(sFS);
}
}
I've tried with MYUSER = the ftp account (to be able to remove it by ftp.
I've tried with MYUSER = IISUSR, if the file had the ownership but without rights.
But I can get rid of the exception.
As far as its a shared server, I have IIS Admin Remote access, but so only on read level; I have access on ftp and so access through C# local execution...
Do you have an idea ?
TY !

FTP transfer via ASP.net page

I'm rather new to development and I have a problem which I haven't sound a solution for. I can't seem to find if it is possible or not to solve, anyway..
I want to create an asp page which would allow a user to download a whole folder from an ftp server. The ftp server itself is not on the same physical server as the asp site. To further complicate the issue is that I want to use either explicit or implicit transfer which I can't seem to work in a browser.
The webpage acts as an intermediary between the client and the ftp server, and is meant to be as user friendly as possible. eg. the user just have to press download and it automatically downloads from the ftp server without the use of an installed local client.
client -> asp page -> ftp server
client <- ftp server
My problem is that the asp page does not have the permission to create files on the client system. I have managed to connect to the ftp and try to download all files in a folder but the files do not appear on the client in the target folder.
I might just be searching for the wrong terms but I would appreciate any feedback.
when you say "client" I assume you are referring to the asp.net server. In that case you need to check what user the app pool for your website is running under. Then you need to make sure that user has write permissions to the folder you are storing the ftp files in.
The user is most likely network service.
Your ASP site will not be able to write directly to the end user's system. Just think, would you want any website to have direct access to your file system?
You could download the files to a temporary folder on the Web Server, and then use write it in a response to prompt the user to download it.
Here is a SO question regarding downloading files in ASP.NET
From our question's comment discussion, looks like you are looking to provide user with a option to download file which you have collected on server side from ftp server
//Connect to your file
FileStream sourceFile = new FileStream(Server.MapPath(#"FileName"), FileMode.Open);
float FileSize= sourceFile.Length;
//Write the file content to a byte Array
byte[] getContent = new byte[(int)FileSize];
sourceFile.Read(getContent, 0, (int)sourceFile.Length);
sourceFile.Close();
//Build HTTP Response object
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Length", getContent.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);
Response.BinaryWrite(getContent);
Response.Flush();
Response.End();
Please see if the above code helps.
Also, check out HTTP Response file Attachment Discussion

C# WebClient Download file from FTP

I am using the following code to download file from FTP.
NetworkCredential credential = new NetworkCredential(Properties.Settings.Default.FTPUserName, Properties.Settings.Default.FTPPassword);
string inputfilepath = Path.Combine(Properties.Settings.Default.LocalDownloadFolder, file);
string ftpfullpath = Properties.Settings.Default.FTPSite + Properties.Settings.Default.FTPFolder + file;
WebClient request1 = new WebClient();
request1.Credentials = credential;
request1.DownloadFile(ftpfullpath, inputfilepath);
Values of the first two vaiables is:
E:\FTPDownloads\CardholderManagementReport_1030_2012-12-11.xls
ftp://abc.com/AKSHAY/CardholderManagementReport_1030_2012-12-11.xls
It shows error as :
The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
EDIT:
I can see that the file is indeed present there, credentials are ok and I can download it using FileZilla
The error 550 returned by the FTP server indicates that the user you are using to try and access the file with does not have permission to access the file.
Either use some other set of credentials that has access to the file or change the permissions on the file to allow access.
This makes it work.
ftp://abc.com/%2f/AKSHAY/CardholderManagementReport_1030_2012-12-11.xls
Explaination is also there.

Download File via Tamir.SharpSSH.Sftp.Get()

I'm trying to download a file using the Tamir SSH library. I'm able to connect to the remote FTP site, upload files to the site, but I'm getting exceptions when trying to download. I've given IIS_IUSRS full control of the local directory as well as ASPNET. I've tested an I'm able to create a text file in the same local directory I'm trying to download to. Any ideas?
string SFTP_HOST = ConfigurationManager.AppSettings["AccentivFtpHost"];
string SFTP_USERNAME = ConfigurationManager.AppSettings["AccentivFtpUsername"];
string SFTP_PASSWORD = ConfigurationManager.AppSettings["AccentivFtpPassword"];
Sftp client = new Sftp(SFTP_HOST, SFTP_USERNAME, SFTP_PASSWORD);
client.Connect(22);
client.Get("test.txt", "c:\\test.txt");
You probably lack a '/' character in the file directory. You may need to put it either in the Get function call before the "test.txt" like "/test.txt" or at the end of your AccentivFtpHost value in the app config file.

Categories