I have a VB Script code, which I want to convert to C#. I am using ScriptTask functionality in SSIS.
I have the existing vb script code as below:
rs.Open "Select AName, LDAP from table", conn
With rs
.movefirst
Do While Not .EOF
strAccName = .FIELDS("AName")
Set objUser = GetObject(.FIELDS("LDAP"))
strLPCDate = objUser.PasswordLastChanged
If Len(strLPCDate) > 0 Then
// do another query
conn.Execute (strSQL)
End If
.movenext
Loop
.Close
End With
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
Always be explicit. Do you mean that it doesn't work or are you looking for other options? If code fails always tell what happens (code can fail for non visible reasons and there is no point to let others to guess which problem you SEE, also it's common
to see or suspect multiple issues and then we don't know 100% with which one to start etc...).
Participant
1443 Points
2030 Posts
Getting PasswordLastChanged attribute from LDAP
Sep 30, 2020 05:41 PM|venkatzeus|LINK
Hi.
I have a VB Script code, which I want to convert to C#. I am using ScriptTask functionality in SSIS.
I have the existing vb script code as below:
strLPCDate = objUser.PasswordLastChanged
How to get the value of PasswordLastChanged in C#
Thanks
Contributor
2990 Points
880 Posts
Re: Getting PasswordLastChanged attribute from LDAP
Oct 01, 2020 01:41 AM|Sean Fang|LINK
Hi venkatzeus,
I recommend using a Code Converter between C# and VB. You could find many similar converters on Google.
One example is the converter from telerik.
Related link: https://converter.telerik.com/
Hope this can help you.
Best regards,
Sean
Participant
1443 Points
2030 Posts
Re: Getting PasswordLastChanged attribute from LDAP
Oct 01, 2020 03:58 AM|venkatzeus|LINK
Hi Sean.
Apologies. I am not looking for a code converter tool. I want to know the equivalent of this logic:
Set objUser = GetObject(.FIELDS("LDAP"))
strLPCDate = objUser.PasswordLastChanged
Participant
1443 Points
2030 Posts
Re: Getting PasswordLastChanged attribute from LDAP
Oct 01, 2020 07:37 AM|venkatzeus|LINK
HI.
Could you please check, if this is correct way.
string objUser = dt.Rows[i]["LDAP"].ToString(); // I have the LDAP value in the datarow
DirectoryEntry user = new DirectoryEntry(objUser);
object passwordLastSet = (long)user.Properties["pwdLastSet"].Value; // is this correct ?
In the existing script, I have code as below, which I want to make in c#:
Dim strLPCDate
Set objUser = GetObject(.FIELDS("LDAP"))
strLPCDate = objUser.PasswordLastChanged
If Len(strLPCDate) > 0 Then
strSQL = "UPDATE table SET column = CONVERT(DATETIME,'" & strLPCDate & "',103) WHERE AName = '" & strAccName & "'"
All-Star
48670 Points
18169 Posts
Re: Getting PasswordLastChanged attribute from LDAP
Oct 01, 2020 08:12 AM|PatriceSc|LINK
Always be explicit. Do you mean that it doesn't work or are you looking for other options? If code fails always tell what happens (code can fail for non visible reasons and there is no point to let others to guess which problem you SEE, also it's common to see or suspect multiple issues and then we don't know 100% with which one to start etc...).
Else if using C#, you could use https://docs.microsoft.com/en-us/dotnet/api/system.directoryservices.accountmanagement.userprincipal.findbyidentity?view=dotnet-plat-ext-3.1 to find the user and then you can use the https://docs.microsoft.com/en-us/dotnet/api/system.directoryservices.accountmanagement.authenticableprincipal.lastpasswordset?view=dotnet-plat-ext-3.1 property (you can also access other properties not exposed directly).
As it seems you are using SQL Server you could also access AD from the server:
https://blog.sqlauthority.com/2016/03/30/sql-server-query-active-directory-data-using-adsi-ldap-linked-server/