Windows Develop Bookmark and Share   
 index > Windows Forms Sample Applications > Windows service to Lock accounts in Database and Active Directory
 

Windows service to Lock accounts in Database and Active Directory

Hi,

I wrote a windows service,that retrives deleted users from database and disable their accounts from Active Directory.

I don't understand why,it is not functioning properly.I will paste my code here.

I am writing into event log when service starts and service ends.I am able to view my messages in Event Log.But accounts are not disebling in Ad.I tried debugging the same code in a normal website,it is executing properly with correct result.So,placed the same functions in Windows services,but no result...........

Here is my Code

Imports System

Imports System.Data

Imports System.Data.SqlClient

Imports System.DirectoryServices

Public Class Service1

Dim ldapstring = "LDAP://192.168.181.2/DC=Spec,DC=Dev"

Dim username As String

Protected Overrides Sub OnStart(ByVal args() As String)

' Add code here to start your service. This method should set things

' in motion so your service can do its work.

DisableAccounts()

EventLog1.WriteEntry(username)

End Sub

Public Sub New()

' This call is required by the Windows.Forms Component Designer.

InitializeComponent()

' TODO: Add any initialization after the InitComponent call

If Not System.Diagnostics.EventLog.SourceExists("Unsubscribe") Then

System.Diagnostics.EventLog.CreateEventSource("Unsubscribe", "UnsubscribeAccount")

End If

EventLog1.Source = "Unsubscribe"

EventLog1.Log = "UnsubscribeAccount"

End Sub

Protected Overrides Sub OnStop()

' Add code here to perform any tear-down necessary to stop your service.

EventLog1.WriteEntry("Disable Unsubscribe Accounts Service Stopped!!")

End Sub

Public Sub DisableAccounts()

Dim dsuser As New DataSet

dsuser = getdata()

Try

If dsuser.Tables(0).Rows.Count > 0 Then

For i As Integer = 0 To dsuser.Tables(0).Rows.Count - 1

If IsDBNull(dsuser.Tables(0).Rows(i).Item("Username")) Then

Dim username As String = dsuser.Tables(0).Rows(i).Item("Username").ToString()

EventLog1.WriteEntry("Disabling user with username:" & " " & username)

DisableADAccount(username)

update(username)

End If

Next

End If

Catch ex As Exception

EventLog1.WriteEntry(ex.StackTrace & " " & "Error: " & ex.Message)

End Try

End Sub

Public Function getdata() As DataSet

Dim strconn As New SqlConnection("Server=192.168.181.13;Database=Content_CustomerDB;user id=content;password=xxxx")

strconn.Open()

Dim query As String = "Select u.Username from cl_users_tbl u,Cl_PrepaidAccountMgtFee_tbl p where u.isactive = '" & True & "' and u.isdeleted='" & True & "' and convert(char,p.paymentenddate,103)='" & Now.Date & "' and u.userid=p.userid"

Dim sqlcmd As New SqlCommand(query, strconn)

Dim da As New SqlDataAdapter(sqlcmd)

Dim ds As New DataSet

da.Fill(ds)

strconn.Close()

Return ds

End Function

Public Sub update(ByVal username As String)

Dim strconn As New SqlConnection("Server=192.168.181.13;Database=Content_CustomerDB;user id=contentl;password=xxxx")

strconn.Open()

Dim sqlcmd As New SqlCommand("Update cl_users_tbl set Isactive='" & False & "'where username='" & username & "'", strconn)

sqlcmd.ExecuteNonQuery()

strconn.Close()

End Sub

Public Sub DisableADAccount(ByVal username As String)

Dim de As DirectoryEntry = GetDirectoryEntry(ldapstring)

Dim ds As DirectorySearcher = New DirectorySearcher(de)

ds.Filter = "(&(objectClass=user) (cn=" & username & "))"

ds.SearchScope = SearchScope.Subtree

Dim results As SearchResult = ds.FindOne()

If Not results Is Nothing Then

Dim deuser As DirectoryEntry = GetDirectoryEntry(results.Path)

Dim val As Integer = CInt(deuser.Properties("userAccountControl").Value)

deuser.Properties("userAccountControl").Value = val Or &H2

' deuser.Properties("msExchHideFromAddressLists").Value = "TRUE"

deuser.CommitChanges()

deuser.Close()

End If

de.Close()

End Sub

Public Shared Function GetDirectoryEntry(ByVal Ldappath As String) As DirectoryEntry

Dim de As DirectoryEntry = New DirectoryEntry()

de.Path = Ldappath

de.Username = "Administrator"

de.Password = "pwd"

de.AuthenticationType = AuthenticationTypes.Secure

Return de

End Function

End Class

adi1980  Tuesday, July 15, 2008 6:58 AM

I found the [roblem.It was with the code instead of using NotIsDbNull i used DBNull

adi1980  Wednesday, July 16, 2008 12:57 AM

I found the [roblem.It was with the code instead of using NotIsDbNull i used DBNull

adi1980  Wednesday, July 16, 2008 12:57 AM

You can use google to search for other answers

Custom Search

More Threads

• IssueVision:Why I can't sign in?
• Creating a XMLFile
• remote server is unreachable or the request has timed out
• Please Help me
• AuthService.asmx
• New idea!
• Server Setup Package Taskvision 2.0
• How come there is no download or synchronize option?
• more information
• Starting out