Managed Code FTP Component Reqd

D

Daren Hawes

Hi,

My web host has a low security setting on the shared .net server I use. I
cannot use third party dll's or install assemblies in the GAC.

All I need to do is verify that a FTP login is true. I do not need to move
files etc, simply check if the ftp details that were collected in a web form
actually authenticate with the remote ftp server. Can this be completed
using managed code only?

Please help
 
M

Martin Dechev

You can use the wininet functions, it is quite easy:

using System;
using System.Runtime.InteropServices;

public class DllImports
{
[DllImport("WinInet.dll", CharSet=CharSet.Auto)]
public static extern IntPtr InternetOpen(
string lpszAgent,
int dwAccessType,
string lpszProxyName,
string lpszProxyBypass,
int dwFlags);

[DllImport("WinInet.dll", CharSet=CharSet.Auto)]
public static extern IntPtr InternetConnect(
IntPtr hInternet,
string lpszServerName,
int nServerPort,
string lpszUsername,
string lpszPassword,
int dwService,
int dwFlags,
IntPtr dwContext);

[DllImport("WinInet.dll")]
public static extern int InternetCloseHandle(IntPtr hInternet);

[DllImport("WinInet.dll", CharSet=CharSet.Auto)]
public static extern int InternetGetLastResponseInfo(
ref int lpdwError,
System.Text.StringBuilder lpszBuffer,
ref int lpdwBufferLength);
}

public class some_aspx : System.Web.UI.Page
{
protected void Page_Load(object s, EventArgs e)
{
IntPtr InternetOpen = DllImports.InternetOpen(
@"Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.2;+.NET+CLR+1.1.4322)",
0,//INTERNET_OPEN_TYPE_PRECONFIG
null,
null,
0);
if(InternetOpen == IntPtr.Zero)
{
Response.Write("InternetOpen failed with errorcode:<br>" +
Marshal.GetLastWin32Error());
return;
}
int a = 724;//Just anything
IntPtr InternetConnect = DllImports.InternetConnect(
InternetOpen,
"your server",
21,//INTERNET_DEFAULT_FTP_PORT
"your username",
"your password",
1,//INTERNET_SERVICE_FTP
0x08000000,//INTERNET_FLAG_PASSIVE
new IntPtr(a));
if(InternetConnect == IntPtr.Zero)
{
Response.Write("InternetConnect failed with errorcode:<br>" +
Marshal.GetLastWin32Error());
int lpdwError = 0, lpdwBufferLength = 2048;
System.Text.StringBuilder lpszBuffer =
new System.Text.StringBuilder(lpdwBufferLength);
DllImports.InternetGetLastResponseInfo(
ref lpdwError,
lpszBuffer,
ref lpdwBufferLength);
Response.Write("<br><br>InternetGetLastResponseInfo:");
Response.Write("<br>Error: " + lpdwError);
Response.Write("<br>Message: " + lpszBuffer.ToString());
DllImports.InternetCloseHandle(InternetOpen);
return;
}
Response.Write("Connection succeeded.");
DllImports.InternetCloseHandle(InternetOpen);
DllImports.InternetCloseHandle(InternetConnect);
}
}

Hope this helps
Martin
 
M

Martin Dechev

I believe the second (the Page) class is quite simple.

the DllImports in VB.NET should look like:

<DllImport("WinInet.dll", EntryPoint := "InternetOpen", _
CharSet := CharSet.Auto, ExactSpelling := True)> _
public shared function InternetOpen( _
lpszAgent as string, _
dwAccessType as int32, _
lpszProxyName as string, _
lpszProxyBypass as string, _
dwFlags as int32) as IntPtr
End function

and so on...

Greetings
Martin
 
D

Daren Hawes

Hi Martin,

I am so sorry, but I have tried to convert to VB. I am a student, and
find convering some things from C# to VB, especially system DLL imports!

Is there anywhere you can point me for a VB version of both the class
and the ASP.NET page?

Thx Daren

In the meantime I will search around...
 
M

Martin Dechev

Imports System
Imports System.Runtime.InteropServices

Public Class DllImports
<DllImport("WinInet.dll", _
EntryPoint:="InternetOpen", _
CharSet:=CharSet.Auto, ExactSpelling:=True)> _
Public Shared Function InternetOpen( _
ByVal lpszAgent As String, _
ByVal dwAccessType As Int32, _
ByVal lpszProxyName As String, _
ByVal lpszProxyBypass As String, _
ByVal dwFlags As Int32) As IntPtr
End Function

<DllImport("WinInet.dll", _
EntryPoint:="InternetConnect", _
CharSet:=CharSet.Auto, ExactSpelling:=True)> _
Public Shared Function InternetConnect( _
ByVal hInternet As IntPtr, _
ByVal lpszServerName As String, _
ByVal nServerPort As Int32, _
ByVal lpszUsername As String, _
ByVal lpszPassword As String, _
ByVal dwService As Int32, _
ByVal dwFlags As Int32, _
ByVal dwContext As IntPtr) As IntPtr
End Function

<DllImport("WinInet.dll", _
EntryPoint:="InternetCloseHandle", _
CharSet:=CharSet.Auto, ExactSpelling:=True)> _
Public Shared Function InternetCloseHandle( _
ByVal hInternet As IntPtr) As Int32
End Function

<DllImport("WinInet.dll", _
EntryPoint:="InternetGetLastResponseInfo", _
CharSet:=CharSet.Auto, ExactSpelling:=True)> _
Public Shared Function InternetGetLastResponseInfo( _
ByRef lpdwError As Int32, _
ByVal lpszBuffer As System.Text.StringBuilder, _
ByRef lpdwBufferLength As Int32) As Int32
End Function

Public Shared Function CheckFtpLogin( _
ByVal FtpServerName As String, _
ByVal FtpUsername As String, _
ByVal FtpPassword As String) As Boolean
Dim InternetOpen, InternetConnect As IntPtr
Dim a As Int32 = 724 'whatever
Dim p As New IntPtr(a)
InternetOpen = DllImports.InternetOpen( _
"Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.2;+.NET+CLR+1.1.4322)", _
0, _
Nothing, _
Nothing, _
0)
If IntPtr.op_Equality(InternetOpen, IntPtr.Zero) Then
'InternetOpen failed.
'Call Marshal.GetLastWin32Error() for the errorcode
Return False
End If
InternetConnect = DllImports.InternetConnect( _
InternetOpen, _
FtpServerName, _
21, _
FtpUsername, _
FtpPassword, _
1, _
&H8000000, _
p)
If IntPtr.op_Equality(InternetConnect, IntPtr.Zero) Then
'InternetOpen failed.
'Call Marshal.GetLastWin32Error() for the errorcode
'Call InternetGetLastResponseInfo to see the response
Dim lpdwError As Int32 = 0
Dim lpdwBufferLength As Int32 = 2048
Dim lpszBuffer As _
New System.Text.StringBuilder(lpdwBufferLength)
DllImports.InternetGetLastResponseInfo( _
lpdwError, lpszBuffer, lpdwBufferLength)
'Free the handle:
DllImports.InternetCloseHandle(InternetOpen)
Return False
End If
' Everything OK
'Free the handles:
DllImports.InternetCloseHandle(InternetOpen)
DllImports.InternetCloseHandle(InternetConnect)
Return True
End Function
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top