Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Conversion of VB(?) code to C#

Status
Not open for further replies.

dbe123

Technical User
Apr 3, 2009
10
GB
Hi,

An ex-worker of our company left the following code:

function AuthenticateUser(UserName, Password, Domain)
dim strUser
' assume failure
AuthenticateUser = false

strUser = UserName
strPassword = Password

strQuery = "SELECT cn FROM 'LDAP://" & Domain & "' WHERE objectClass='*' "
set oConn = server.CreateObject("ADODB.Connection")
oConn.Provider = "ADsDSOOBJECT"
oConn.Properties("User ID") = strUser
oConn.Properties("Password") = strPassword
oConn.Properties("Encrypt Password") = true
oConn.open "DS Query", strUser, strPassword

set cmd = server.CreateObject("ADODB.Command")
set cmd.ActiveConnection = oConn
cmd.CommandText = strQuery
on error resume next
set oRS = cmd.Execute
if oRS.bof or oRS.eof then
AuthenticateUser = false
else
AuthenticateUser = true
end if
set oRS = nothing
set oConn = nothing

end function

Due to upgrade issues, this code needs to be converted to C#.

Can anyone help me with that?

Thanks!
 
looks like they are writing a sql statement against active directory (or another ldap storage) to determine if the user exists. you can do this with ado.net, but it's better to use the DirectoryServices components. That's what its designed for.

depending on how or why this is used you may be able to get ride of this code altogether and take advantage of Integrated Windows Authentication.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Hi Jason,

Thanks for your reply.

I've heard about the new DirectoryServices which are built in in the .NET Framework, however I have no knowledge how to use them, or how to access them at all.

I should probably study a good tutorial about this matter, which will cost precious time.

In order to gain a 'quick & dirty win' in this matter, I would prefer to convert this code 'as is'.

However, I'm stuck at the oConn.Properties statement in the code.
C# doesn't seem to accept this.

Any ideas?
 
What library(ies) does this code import?
It probably has:
Imports System.Data
and/or
Imports System.Data.OleDb

You will need to import the same into your C# code.
 
Hi Jges,

Those libraries are imported in the new solution.
I also added a reference to the ADODB library.

That's not the problem I think.
 
you won't need adodb that is legacy API.

this code specifically is not a good example of how ado.net was meant to be used. here the code says... if you can open the connection you are a valid user. otherwise you are not. to code that it would look something like this
Code:
var connectionstring = "provider=ADsDSOOBJECT;...ldap domain;username=...;password=...;"
using(var connection = new OleDbConnection(connectionString))
{
   try
   {
      connection.Open();
      return true;
   }
   catch ([Specific Ole Exception] e)
   {
      //log exception
      return false;
   }
}


The move from vb 6 to C# will have some difficulties. it's not a quick and dirty process to upgrade. and if you're just changing the syntax from vb to C# there isn't much gain either.

In order to gain a 'quick & dirty win' in this matter, I would prefer to convert this code 'as is'.
this will cost you in the long run. unless you can turn around right after deployment and begin to properly code the system.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Looks like it could be VBScript code, not that that helps you.
 
Hi Jason,

That's exactly what the function needs to do.

All of the valid users are grouped in one specific Active Directory Group.
If the user can login to this group, he is authenticated to use the web application.

If an error occurs, the user must be rejected.

I agree that the code isn't clean, and rework needs to be done ASAP.
This will be one of my main tasks in the coming weeks.

Thanks for the help!
 
Hi guys,

Still some issues with the above.

It seems like the C# function always returns
a TRUE value, regardless of which user is logged in.
In other words, all users are authorised (which is certainly not the case).

A collegue of mine (who is an expert PHP programmer) supplied me with the following working code:

// connect to ldap server
$ldapconn = ldap_connect($server, "389")
or die("Could not connect to LDAP server.");

if ($ldapconn) {
// first filter username
$filter = "(&(objectclass=organizationalPerson)(&(memberOf=" . $groupdn . "," . $basedn . ")(&(sAMAccountName=" . $testname . "))))";
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
//echo $filter;
// binding to ldap server
$ldapbind = ldap_bind($ldapconn, $username, $password);

// verify binding
if ($ldapbind) {
echo "LDAP bind successful...<br /><hr /><br />";
$result = ldap_search($ldapconn, $basedn, $filter) or die ("Search error.");
$entries = ldap_get_entries($ldapconn, $result);
$binddn = $entries[0]["dn"];
echo "<p>Bind DN found: ". $binddn . "</p>";
echo "<hr />";

// Bind again using the DN retrieved. If this bind is successful,
// then the user has managed to authenticate.
$ldapbind = ldap_bind($ldapconn, $binddn, $testpwd);
if ($ldapbind) {
echo "Successful authentication for " . $binddn . ".";
} else {
echo "Failed authentication for " . $binddn . ".";
}
ldap_close($ldapconn);
} else {
echo "Could not bind anonymously...";
}
}

Now I'm trying to convert this to my C# application.

I'm having trouble finding the right commands and syntax for these PHP functions.

Anyone has a hint how to do this?
For example: how do you convert the function ldap_set_option(...) to C#?

Thanks in advance
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top