469,950 Members | 2,389 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

Another block of C# code to convert

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.
Jun 27 '08 #1
6 1328

"Ty" <tb*****@lewistownhospital.orgwrote in message
news:8b**********************************@59g2000h sb.googlegroups.com...
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.ne...Converter.aspx

LS

Jun 27 '08 #2
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" <a@b.cschreef in bericht
news:%2****************@TK2MSFTNGP03.phx.gbl...
>
"Ty" <tb*****@lewistownhospital.orgwrote in message
news:8b**********************************@59g2000h sb.googlegroups.com...
>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.ne...Converter.aspx

LS
Jun 27 '08 #3
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[MVP]" wrote:
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" <a@b.cschreef in bericht
news:%2****************@TK2MSFTNGP03.phx.gbl...

"Ty" <tb*****@lewistownhospital.orgwrote in message
news:8b**********************************@59g2000h sb.googlegroups.com...
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.ne...Converter.aspx

LS
Jun 27 '08 #4
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" <Da********@discussions.microsoft.comschreef in bericht
news:C2**********************************@microsof t.com...
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[MVP]" wrote:
>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" <a@b.cschreef in bericht
news:%2****************@TK2MSFTNGP03.phx.gbl...
>
"Ty" <tb*****@lewistownhospital.orgwrote in message
news:8b**********************************@59g2000h sb.googlegroups.com...
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.ne...Converter.aspx

LS
Jun 27 '08 #5
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]

Jun 29 '08 #6
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 )

Jun 29 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

188 posts views Thread by christopher diggins | last post: by
2 posts views Thread by Alex | last post: by
reply views Thread by David C | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.