locked
Active Directory username RRS feed

  • Question

  • User-1862284125 posted

    return HttpContext.Current.User.Identity.Name.ToString()

    I use this to get the username (example JSMITH) of the logged in user, but now a situation has come up where I need the actual name of the user, First and Last.

    Is there a way to pull that from the PC?

    Tuesday, May 16, 2006 11:47 AM

All replies

  • User1354132231 posted
    Yes, use the name as the search key to find the user in AD and return more information.

    Here is a sample post that shows how.
    Tuesday, May 16, 2006 4:26 PM
  • User-1862284125 posted

    I read most the replies.. and my script keeps dying on this line...

     

    using (SearchResultCollection src = ds.FindAll())

     

    <%@ Assembly Name="System.DirectoryServices, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"%>

    <%@ Import Namespace="System.DirectoryServices" %>

    <%@ Import Namespace="System.Text" %>

    <HTML>

    <HEAD>

    <script language="c#" runat="server">

    private void Page_Load(object sender, System.EventArgs e)

    {

    if(!Page.IsPostBack)

    {

    string adPath = "LDAP://postoffice/DC=mbank,DC=com";

    string sFilter = String.Format(

    "(&(objectCategory=person)(sAMAccountName={0}))",

    User.Identity.Name.Split(new char[]{'\\'})[1]

    );

    using (DirectoryEntry de = new DirectoryEntry(adPath))

    {

    de.AuthenticationType = AuthenticationTypes.Secure;

    de.Username = "logon";

    de.Password = "password";

    string[] attribs = new string[]{"mail", "displayName"};

    DirectorySearcher ds = new DirectorySearcher(

    de,

    sFilter,

    attribs //leave this null to return all attribs

    );

    using (SearchResultCollection src = ds.FindAll())

    {

    SearchResult sr = null;

    if (src.Count > 0)

    sr = src[0];

    if(sr == null)

    {

    msg.Text = "User not found...";

    return;

    }

    StringBuilder sb = new StringBuilder();

    foreach (string key in attribs)

    {

    if(sr.Properties.Contains(key))

    {

    foreach(object o in sr.Properties[key])

    {

    sb.AppendFormat("{0}: {1}<br>", key, o);

    }

    }

    }

    msg.Text = sb.ToString();

    }

    }

    }

    }

     

    </script>

    </HEAD>

    <BODY>

    <form id="main" runat="server">

    Info for user: <b><%=User.Identity.Name%></b> <br><br>

    <asp:label id="msg" runat="server" />

    </form>

    </BODY>

    </HTML>

    Tuesday, May 16, 2006 5:08 PM