By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,108 Members | 1,750 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,108 IT Pros & Developers. It's quick & easy.

Managed Code FTP Component Reqd

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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+CL R+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>InternetGetLastResponseInf o:");
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
"Daren Hawes" <da***@webdesignmagic.com.au> wrote in message
news:uR**************@TK2MSFTNGP12.phx.gbl...
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

Nov 18 '05 #2

P: n/a


Is there a VB version around?

Thanks Daren

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #3

P: n/a


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #4

P: n/a
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
"Daren Hawes" <da***@webdesignmagic.com.au> wrote in message
news:OS**************@TK2MSFTNGP11.phx.gbl...


Is there a VB version around?

Thanks Daren

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #5

P: n/a


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...

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #6

P: n/a
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+CL R+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

"Daren Hawes" <da***@webdesignmagic.com.au> wrote in message
news:#G**************@TK2MSFTNGP10.phx.gbl...


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...

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.