Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am a newbie in .net and need your help gurus to finalize my code. Your helpf will be much appreciated.

Here is the scenario:

A user can register using the format eg. name@ourdomain.com. Usernames are their registered name in our domain. Registration will not be accepted unless they have their names existed in our domain. During registration, username will be validated against their names existed in the domain. A list of all registered names under our domain eg. name@domain.com will be saved from our sql database.

Can someone help me with this please?

Thanks in advance for anyone who can share.

Eddie
Posted
Comments
Miguelribeiro 25-Oct-12 4:05am    
do you want to validate, a user against your domain before saving the record to sql ?

yes.user can be accepted his/her registration after only validating entry against the one existed in our domain.
 
Share this answer
 
This can be achieved using an LDAP Query to your AD,

here is a link which will help you achieve your requirement

http://www.ianatkinson.net/computing/adcsharp.htm[^]
 
Share this answer
 
thanks for your reply but i need to get deep into that. actually here is the code for logging in but i need to verify this login entry against the usernames in AD. can you share your code so i can manipulate it to fit my requirements?


protected void log_Click(object sender, EventArgs e)
{
SqlDataSource sds = new SqlDataSource();
sds.ConnectionString = ConfigurationManager.ConnectionStrings["myDbConnectionString1"].ToString();

sds.SelectParameters.Add("username", TypeCode.String, this.username.Text);
sds.SelectParameters.Add("password", TypeCode.String, this.password.Text);

sds.SelectCommand = "SELECT * FROM [myTb] WHERE [username] = @username AND [password] = @password";

DataView dv = (DataView)sds.Select(DataSourceSelectArguments.Empty);
try
{
if (dv.Count == 0)
{
this.lblinfo.ForeColor = System.Drawing.Color.Red;
this.lblinfo.Text = "Invalid username and password!";
return;
}
else
{
FormsAuthentication.RedirectFromLoginPage(username.Text, true);
Response.Redirect("Auth/Upload.aspx");
}
}
catch
{

}
finally
{

}
}
 
Share this answer
 
how can i use my laptop to serve as an LDAP server?
 
Share this answer
 
C#
using System.DirectoryServices;

    //srvr = ldap server, e.g. LDAP://yourdomain.com
    //usr = user name of the person you are trying to autenticate,
    //pwd = user password of the person you are trying to autenticate,
    public bool IsAuthenticated(string srvr, string usr, string pwd)
    {
        bool authenticated = false;

        try
        {
            DirectoryEntry entry = new DirectoryEntry(srvr, usr, pwd);
            object nativeObject = entry.NativeObject;
            authenticated = true;
        }
        catch (DirectoryServicesCOMException cex)
        {
            //not authenticated; reason why is in cex
        }
        catch (Exception ex)
        {
            //not authenticated due to some other exception [this is optional]
        }
        return authenticated;
    }
 
Share this answer
 

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