Another block of C# code to convert

T

Ty

Here is another block that I can not get to convert. This is a class
module.

public bool IsAuthenticated(string domain, string username, string
pwd)
{
string domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry( _path,
domainAndUsername, pwd);
try
{
// Bind to the native AdsObject to force authentication.
Object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if(null == result)
{
return false;
}
// Update the new path to the user in the directory
_path = result.Path;
_filterAttribute = (String)result.Properties["cn"][0];
}
catch (Exception ex)
{
throw new Exception("Error authenticating user. " + ex.Message);
}
return true;
}

}

Any help would be apperciated.
 
L

Lloyd Sheen

Ty said:
Here is another block that I can not get to convert. This is a class
module.

public bool IsAuthenticated(string domain, string username, string
pwd)
{
string domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry( _path,
domainAndUsername, pwd);
try
{
// Bind to the native AdsObject to force authentication.
Object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if(null == result)
{
return false;
}
// Update the new path to the user in the directory
_path = result.Path;
_filterAttribute = (String)result.Properties["cn"][0];
}
catch (Exception ex)
{
throw new Exception("Error authenticating user. " + ex.Message);
}
return true;
}

}

Any help would be apperciated.


Public Function IsAuthenticated(ByVal domain As String, ByVal username As
String, ByVal pwd As String) As Boolean
Dim domainAndUsername As String = domain + "\" + username
Dim entry As New DirectoryEntry(_path, domainAndUsername, pwd)
Try
' Bind to the native AdsObject to force authentication.
Dim obj As Object = entry.NativeObject
Dim search As New DirectorySearcher(entry)
search.Filter = "(SAMAccountName=" + username + ")"
search.PropertiesToLoad.Add("cn")
Dim result As SearchResult = search.FindOne()
If result Is Nothing Then
Return False
End If
' Update the new path to the user in the directory
_path = result.Path
_filterAttribute = DirectCast(result.Properties("cn")(0), String)
Catch ex As Exception
Throw New Exception("Error authenticating user. " + ex.Message)
End Try
Return True
End Function


http://codeconverter.sharpdevelop.net/SnippetConverter.aspx

LS
 
C

Cor Ligthert[MVP]

Hi Seth,

Just for fun.

I saw your code and made this from it, looks for me more as VB 2008

I hope Anton will show what it does with his converter, I expect that it is
better than you showed with those plusses.

\\\
Option Infer On
Option Strict On
Option Explicit On
Imports System.DirectoryServices

Module Module1
Sub Main()
End Sub
Public Function IsAuthenticated(ByVal domain As String, ByVal username
As String, ByVal pwd As String) As Boolean
Dim path, filterAttribute As String 'dummies
Dim domainAndUsername = domain & "\" & username
Dim entry As New DirectoryEntry(path, domainAndUsername, pwd)
Try
' Bind to the native AdsObject to force authentication.
Dim obj = entry.NativeObject
Dim search As New DirectorySearcher(entry)
search.Filter = "(SAMAccountName=" & username & ")"
search.PropertiesToLoad.Add("cn")
Dim result = search.FindOne()
If result IsNot Nothing Then
'Update the new path to the user in the directory
path = result.Path
filterAttribute = result.Properties("cn")(0).ToString
Return True
Else
Return False
End If
Catch ex As Exception
Throw New Exception("Error authenticating user. " & ex.Message)
End Try
End Function
End Module
///


(I did not test the result, only changed the code)

Cor

Lloyd Sheen said:
Ty said:
Here is another block that I can not get to convert. This is a class
module.

public bool IsAuthenticated(string domain, string username, string
pwd)
{
string domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry( _path,
domainAndUsername, pwd);
try
{
// Bind to the native AdsObject to force authentication.
Object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if(null == result)
{
return false;
}
// Update the new path to the user in the directory
_path = result.Path;
_filterAttribute = (String)result.Properties["cn"][0];
}
catch (Exception ex)
{
throw new Exception("Error authenticating user. " + ex.Message);
}
return true;
}

}

Any help would be apperciated.


Public Function IsAuthenticated(ByVal domain As String, ByVal username As
String, ByVal pwd As String) As Boolean
Dim domainAndUsername As String = domain + "\" + username
Dim entry As New DirectoryEntry(_path, domainAndUsername, pwd)
Try
' Bind to the native AdsObject to force authentication.
Dim obj As Object = entry.NativeObject
Dim search As New DirectorySearcher(entry)
search.Filter = "(SAMAccountName=" + username + ")"
search.PropertiesToLoad.Add("cn")
Dim result As SearchResult = search.FindOne()
If result Is Nothing Then
Return False
End If
' Update the new path to the user in the directory
_path = result.Path
_filterAttribute = DirectCast(result.Properties("cn")(0), String)
Catch ex As Exception
Throw New Exception("Error authenticating user. " + ex.Message)
End Try
Return True
End Function


http://codeconverter.sharpdevelop.net/SnippetConverter.aspx

LS
 
D

David Anton

Except for using '&' instead of '+' (which may be ambiguous in some cases),
our conversion was quite similar. One difference is that 'DirectCast' is not
the general purpose equivalent for the C# casting operator - CType is a more
reliable replacement (google on DirectCast to see why).
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
VB to Java
Java to VB & C#
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++: VB, C#, or Java to C++/CLI


Cor Ligthert said:
Hi Seth,

Just for fun.

I saw your code and made this from it, looks for me more as VB 2008

I hope Anton will show what it does with his converter, I expect that it is
better than you showed with those plusses.

\\\
Option Infer On
Option Strict On
Option Explicit On
Imports System.DirectoryServices

Module Module1
Sub Main()
End Sub
Public Function IsAuthenticated(ByVal domain As String, ByVal username
As String, ByVal pwd As String) As Boolean
Dim path, filterAttribute As String 'dummies
Dim domainAndUsername = domain & "\" & username
Dim entry As New DirectoryEntry(path, domainAndUsername, pwd)
Try
' Bind to the native AdsObject to force authentication.
Dim obj = entry.NativeObject
Dim search As New DirectorySearcher(entry)
search.Filter = "(SAMAccountName=" & username & ")"
search.PropertiesToLoad.Add("cn")
Dim result = search.FindOne()
If result IsNot Nothing Then
'Update the new path to the user in the directory
path = result.Path
filterAttribute = result.Properties("cn")(0).ToString
Return True
Else
Return False
End If
Catch ex As Exception
Throw New Exception("Error authenticating user. " & ex.Message)
End Try
End Function
End Module
///


(I did not test the result, only changed the code)

Cor

Lloyd Sheen said:
Ty said:
Here is another block that I can not get to convert. This is a class
module.

public bool IsAuthenticated(string domain, string username, string
pwd)
{
string domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry( _path,
domainAndUsername, pwd);
try
{
// Bind to the native AdsObject to force authentication.
Object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if(null == result)
{
return false;
}
// Update the new path to the user in the directory
_path = result.Path;
_filterAttribute = (String)result.Properties["cn"][0];
}
catch (Exception ex)
{
throw new Exception("Error authenticating user. " + ex.Message);
}
return true;
}

}

Any help would be apperciated.


Public Function IsAuthenticated(ByVal domain As String, ByVal username As
String, ByVal pwd As String) As Boolean
Dim domainAndUsername As String = domain + "\" + username
Dim entry As New DirectoryEntry(_path, domainAndUsername, pwd)
Try
' Bind to the native AdsObject to force authentication.
Dim obj As Object = entry.NativeObject
Dim search As New DirectorySearcher(entry)
search.Filter = "(SAMAccountName=" + username + ")"
search.PropertiesToLoad.Add("cn")
Dim result As SearchResult = search.FindOne()
If result Is Nothing Then
Return False
End If
' Update the new path to the user in the directory
_path = result.Path
_filterAttribute = DirectCast(result.Properties("cn")(0), String)
Catch ex As Exception
Throw New Exception("Error authenticating user. " + ex.Message)
End Try
Return True
End Function


http://codeconverter.sharpdevelop.net/SnippetConverter.aspx

LS
 
C

Cor Ligthert[MVP]

David,

That was the most important for me,
Except for using '&' instead of '+' (which may be ambiguous in some
cases),
our conversion was quite similar.

I made the code particulary for 2008 you should not expect that from a
converter

The guy who did the casting in C# with (string)bla did of course as well not
so a good documentative job, as a ToString() from object is one of the most
basic elements of dotnet.

Cor



David Anton said:
Except for using '&' instead of '+' (which may be ambiguous in some
cases),
our conversion was quite similar. One difference is that 'DirectCast' is
not
the general purpose equivalent for the C# casting operator - CType is a
more
reliable replacement (google on DirectCast to see why).
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
VB to Java
Java to VB & C#
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++: VB, C#, or Java to C++/CLI


Cor Ligthert said:
Hi Seth,

Just for fun.

I saw your code and made this from it, looks for me more as VB 2008

I hope Anton will show what it does with his converter, I expect that it
is
better than you showed with those plusses.

\\\
Option Infer On
Option Strict On
Option Explicit On
Imports System.DirectoryServices

Module Module1
Sub Main()
End Sub
Public Function IsAuthenticated(ByVal domain As String, ByVal
username
As String, ByVal pwd As String) As Boolean
Dim path, filterAttribute As String 'dummies
Dim domainAndUsername = domain & "\" & username
Dim entry As New DirectoryEntry(path, domainAndUsername, pwd)
Try
' Bind to the native AdsObject to force authentication.
Dim obj = entry.NativeObject
Dim search As New DirectorySearcher(entry)
search.Filter = "(SAMAccountName=" & username & ")"
search.PropertiesToLoad.Add("cn")
Dim result = search.FindOne()
If result IsNot Nothing Then
'Update the new path to the user in the directory
path = result.Path
filterAttribute = result.Properties("cn")(0).ToString
Return True
Else
Return False
End If
Catch ex As Exception
Throw New Exception("Error authenticating user. " &
ex.Message)
End Try
End Function
End Module
///


(I did not test the result, only changed the code)

Cor

Lloyd Sheen said:
Here is another block that I can not get to convert. This is a class
module.

public bool IsAuthenticated(string domain, string username, string
pwd)
{
string domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry( _path,
domainAndUsername, pwd);
try
{
// Bind to the native AdsObject to force authentication.
Object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if(null == result)
{
return false;
}
// Update the new path to the user in the directory
_path = result.Path;
_filterAttribute = (String)result.Properties["cn"][0];
}
catch (Exception ex)
{
throw new Exception("Error authenticating user. " + ex.Message);
}
return true;
}

}

Any help would be apperciated.


Public Function IsAuthenticated(ByVal domain As String, ByVal username
As
String, ByVal pwd As String) As Boolean
Dim domainAndUsername As String = domain + "\" + username
Dim entry As New DirectoryEntry(_path, domainAndUsername, pwd)
Try
' Bind to the native AdsObject to force authentication.
Dim obj As Object = entry.NativeObject
Dim search As New DirectorySearcher(entry)
search.Filter = "(SAMAccountName=" + username + ")"
search.PropertiesToLoad.Add("cn")
Dim result As SearchResult = search.FindOne()
If result Is Nothing Then
Return False
End If
' Update the new path to the user in the directory
_path = result.Path
_filterAttribute = DirectCast(result.Properties("cn")(0), String)
Catch ex As Exception
Throw New Exception("Error authenticating user. " + ex.Message)
End Try
Return True
End Function


http://codeconverter.sharpdevelop.net/SnippetConverter.aspx

LS
 
M

Michel Posseth

Ty schreef:
Here is another block that I can not get to convert. This is a class
module.

public bool IsAuthenticated(string domain, string username, string
pwd)
{
string domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry( _path,
domainAndUsername, pwd);
try
{
// Bind to the native AdsObject to force authentication.
Object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if(null == result)
{
return false;
}
// Update the new path to the user in the directory
_path = result.Path;
_filterAttribute = (String)result.Properties["cn"][0];
}
catch (Exception ex)
{
throw new Exception("Error authenticating user. " + ex.Message);
}
return true;
}

}

Any help would be apperciated.


You mean you posted a part of a class module without telling wich
references were set and without providing the class scoped variabels

The next time provide this information , maybe this is the reasson why
nobody bothered until so far cause the code is pretty simple .


first of all set a refernce to system.DirectoryServices

then in the top of the code file you put this import statement

Imports System.DirectoryServices


Private _path As String
Private _filterAttribute As String


Public Function IsAuthenticated(ByVal domain As String, ByVal
username As String, ByVal pwd As String) As Boolean
Dim domainAndUsername As String = domain + "\" + username
Dim entry As New DirectoryEntry(_path, domainAndUsername, pwd)
Try
' Bind to the native AdsObject to force authentication.
Dim obj As Object = entry.NativeObject
Dim search As New DirectorySearcher(entry)
search.Filter = "(SAMAccountName=" + username + ")"
search.PropertiesToLoad.Add("cn")
Dim result As SearchResult = search.FindOne()
If result Is Nothing Then
Return False
End If
' Update the new path to the user in the directory
_path = result.Path
_filterAttribute =
DirectCast(result.Properties("cn")(0),String)
Catch ex As Exception
Throw New Exception("Error authenticating user. " + ex.Message)
End Try
Return True
End Function

regards
Michel Posseth [MCP]
 
M

Michel Posseth

Michel Posseth schreef:
Ty schreef:
Here is another block that I can not get to convert. This is a class
module.

public bool IsAuthenticated(string domain, string username, string
pwd)
{
string domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry( _path,
domainAndUsername, pwd);
try
{
// Bind to the native AdsObject to force authentication.
Object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if(null == result)
{
return false;
}
// Update the new path to the user in the directory
_path = result.Path;
_filterAttribute = (String)result.Properties["cn"][0];
}
catch (Exception ex)
{
throw new Exception("Error authenticating user. " + ex.Message);
}
return true;
}

}

Any help would be apperciated.


You mean you posted a part of a class module without telling wich
references were set and without providing the class scoped variabels

The next time provide this information , maybe this is the reasson why
nobody bothered until so far cause the code is pretty simple .


first of all set a refernce to system.DirectoryServices

then in the top of the code file you put this import statement

Imports System.DirectoryServices


Private _path As String
Private _filterAttribute As String


Public Function IsAuthenticated(ByVal domain As String, ByVal
username As String, ByVal pwd As String) As Boolean
Dim domainAndUsername As String = domain + "\" + username
Dim entry As New DirectoryEntry(_path, domainAndUsername, pwd)
Try
' Bind to the native AdsObject to force authentication.
Dim obj As Object = entry.NativeObject
Dim search As New DirectorySearcher(entry)
search.Filter = "(SAMAccountName=" + username + ")"
search.PropertiesToLoad.Add("cn")
Dim result As SearchResult = search.FindOne()
If result Is Nothing Then
Return False
End If
' Update the new path to the user in the directory
_path = result.Path
_filterAttribute =
DirectCast(result.Properties("cn")(0),String)
Catch ex As Exception
Throw New Exception("Error authenticating user. " + ex.Message)
End Try
Return True
End Function

regards
Michel Posseth [MCP]
hmmm on this computer i installed Thunderbird ( just for testing :) )
but it looks like i am a bit behind reality ( thought no one had
answered this one yet )
 

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

Top