Click here to Skip to main content
14,924,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The following are the code I am using to fetch active directory.
you can see below the users should manually need to type their username to retrieve their details.
dssearch.Filter = "(sAMAccountName=" + txtusername.Text + ")";

but I want to make this auto fetch, so when user log on it should capture their sAMAccountName and fetch it immediately in the screen.

please help me...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.DirectoryServices;

public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)

    protected void btnSubmit_Click(object sender, EventArgs e)
            string connection = ConfigurationManager.ConnectionStrings["ADConnection"].ToString();
            DirectorySearcher dssearch = new DirectorySearcher(connection);
            //dssearch.Filter = "(sAMAccountName=" + txtusername.Text + ")";
            dssearch.Filter = "(sAMAccountName=" + txtusername.Text + ")";
            SearchResult sresult = dssearch.FindOne();
            DirectoryEntry dsresult = sresult.GetDirectoryEntry();
            lblfname.Text = dsresult.Properties["givenName"][0].ToString();
            lbllname.Text = dsresult.Properties["sn"][0].ToString();
            lblemail.Text = dsresult.Properties["mail"][0].ToString();
            lbldisplayname.Text = dsresult.Properties["displayName"][0].ToString();
            lbltelephone.Text = dsresult.Properties["telephoneNumber"][0].ToString();
            lblusername.Text = dsresult.Properties["userPrincipalName"][0].ToString();
          lbldepartment.Text = dsresult.Properties["department"][0].ToString();
          lblmanager.Text = dsresult.Properties[ "title"][0].ToString();
           //lblmanager.Text = dsresult.Properties["manager"][0].ToString();

Updated 14-Aug-13 3:11am
David_Wimbley 14-Aug-13 9:46am
So this is really stupid question. You've got all that code in your submit button click event and you want it to be there when the user logs in. Why not move it to the Page Load method? Or is that just to easy?

1 solution

You need to pull the user name from Active Directory(AD). Then use it to auto-login to your applicaiton.

Since this is a webpage with AD running and IIS is set to allow it Windows authentication, then you get the user's domain and id a few ways. You have the Page.Identity where you can get it or you can get it form the HttpContext if you builda user class that handles all of this for you, which is what I have. I created a class library with all of this so that I can just include it my project and always have access to the user object and automatically authenticated through AD and expose all of the user properties that I need.

Here's an example, you can use something like the following. Play with this code and take a look at the objects and what they contain. If you are not gettting anything from these, that means that IIS is blocking it. Also, if you are putting this code in the web page, you will probalby need to change HTTPContext to Page.Identity. But this should at least point you in the right direction and I think pushing it into another class is probalby the better approached, so I have include that type of code below.

var id = HttpContext.Current.User.Identity;
var domain = HttpContext.Current.User.Identity.Name.Split("\"c)(0);
var username = HttpContext.Current.User.Identity.Name.Split("\"c)(1);

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