Click here to Skip to main content
15,915,702 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Good day all.

I'm working on user management and it requires that I will register user that are in the AD from one of the units. This is to ensure that only those that are in the AD and are in the application DB will be using the application (double authentication).

My problem is that in the course of registering users into the application DB, I don't know how to check for user's name in the AD without the user's password. The fields for registeration are: Username, Password, Rank, Unit. I want to remove this password field but if I remove it the application will be registering those that are not in the AD.

My code is as shown below:
 Protected Sub btnButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnButton1.Click
 Try
  
 If getOrder() = False Then Return
  
 
Dim entry As New DirectoryEntry("LDAP://IP Address")
 entry.Username = txtUsername.Text + "@gov.net"
 entry.Password = txtPassword.Text
  
 Dim searcher As New DirectorySearcher("(&(objectCategory=Person)(objectClass=User))")
  
 searcher.SearchRoot = entry
 searcher.PropertiesToLoad.Add("cn")
  
 Dim result As SearchResult
  
 searcher.Filter = "(mail=net\" & txtUsername.Text & "@gov.net)"
  
 result = searcher.FindOne
  
 Dim sql As String = "INSERT INTO Table (Role,Username,Unit,Rank) VALUES ( '" & _
 ddlRole.Text & "','" & txtUsername.Text.Replace("'", "''") & "','" & _ ddlUnit.Text & "','" & ddlRank.Text & "')"
  
 Dim gentool As New functions
 If gentool.ExecuteDatabase(sql) = True Then
 lblMsg.Text = "Registered successfully"
 goClear()
 Else
 lblMsg.Text = "User already exists"
 End If
  
 Catch ex As Exception
 lblMsg.Text = "Registration failure: Either username or password" 'ex.Message
 End Try
 End Sub
Posted
Updated 17-Apr-13 4:09am
v2
Comments
Espen Harlinn 17-Apr-13 10:29am    
First time doing anything related to security? You're well on your way to implementing several of the OWASP Top 10

1 solution

Yeah, this is a horror story on many levels. For instance, that little Text.Replace() thing you're doing is laughable. It's not going to prevent injection attacks from user input and it can, and will, lead to your code crashing depending on what the user typed. Google for ".net parameterized queries" for a ton of examples and discussion on what you should be doing.

If you want to authenticate someone against AD, you need their password. It's that simple.

If you want to see if someone is IN ActiveDirectory, then you just have to do an LDAP search for their account name, no user password required. This code WILL need to be running under an account that has read permissions to AD though. Even so, this, of course, won't tell if the account is expired or locked without additional work on your part, but hey, it's your job, not mine.
 
Share this answer
 
v2
Comments
Espen Harlinn 17-Apr-13 14:48pm    
5'ed!
DOZDOZ 19-Apr-13 10:12am    
Hello Dave Kreskowiak,

Thanks for having time for this my issue.

But the point is that the client still want me to register user without requesting for his/her password. Please, I therefore request for more contribution to enable me actualize this feature.
Dave Kreskowiak 19-Apr-13 10:37am    
OK, it sounds as though you're requirements and business rules are completely at odds with each other, or your business rules are garbage.

But, since you apparently don't need to authenitcate against AD nor apparently have to validate the users credentials against AD, what's stopping you from using the code you already have to insert the user into the database??
DOZDOZ 22-Apr-13 10:20am    
I'm authenticating against AD and equally validating the user credentials agaisnt the AD. My problem is that I want to register a user without requesting for his/her password on the form.
Dave Kreskowiak 22-Apr-13 10:22am    
SO what's stopping you? You already have the registration code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900