Tuesday, March 19, 2013

NameSpace Required :
using System.DirectoryServices;

public string GetConnection()
        {
            string adsPath = "";
            try
            {
                DirectoryEntry root = new DirectoryEntry("LDAP://RootDSE");
                using (root)
                {
                    string dnc = root.Properties["defaultNamingContext"][0].ToString();
                    string server = root.Properties["dnsHostName"][0].ToString();
                    adsPath = String.Format(
                   "LDAP://{0}/{1}",
                   server,
                   dnc
                   );
                    return adsPath;
                }
            }
            catch (Exception ex)
            {

                return adsPath;
            }
        }


 public void GetUserDetails()
        {
            try
            {
                //string connection = "LDAP://PEACOCK.corpnet.co.in/DC=corpnet,DC=co,DC=in";//ConfigurationManager.ConnectionStrings["ADConnection"].ToString();
                string connection = GetConnectionString();
                if (!string.IsNullOrEmpty(connection))
                {
                    using (HostingEnvironment.Impersonate())
                    {
                        DirectorySearcher dssearch = new DirectorySearcher(connection);
                        if (!string.IsNullOrEmpty(SPContext.Current.Web.CurrentUser.LoginName))
                        {
                            string LoginName = SPContext.Current.Web.CurrentUser.LoginName.Split('\\')[1];
                            dssearch.Filter = "(sAMAccountName=" + LoginName + ")";

                            SearchResult sresult = dssearch.FindOne();
                            DirectoryEntry dsresult = sresult.GetDirectoryEntry();
                            if (sresult != null)
                            {
                              
                                /* First Name Value + Last Name Value*/
                                if (dsresult.Properties["givenname"].Count > 0 && dsresult.Properties["sn"].Count > 0)
                                {
                                    lblRequestedBy.Text = dsresult.Properties["givenname"][0].ToString() + " " + dsresult.Properties["sn"][0].ToString();
                                    lblLastModifiedBy.Text = dsresult.Properties["givenname"][0].ToString() + " " + dsresult.Properties["sn"][0].ToString();
                                }
                                if (dsresult.Properties["mail"].Count > 0)
                                {
                                    /* Email Value*/
                                    lblemail.Text = dsresult.Properties["mail"][0].ToString();
                                }
                                if (dsresult.Properties["telephoneNumber"].Count > 0)
                                {
                                    /* contact No  Value*/
                                    lblcontactno.Text = dsresult.Properties["telephoneNumber"][0].ToString();
                                }
                                if (dsresult.Properties["department"].Count > 0)
                                {
                                    /*Department Value*/
                                    lbldep.Text = dsresult.Properties["department"][0].ToString();
                                }
                                //}
                            }
                            else
                            {
                             Response.Write("Login Does not Exists & Records does not found");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
              throw ex;
            }
        }

No comments:

Post a Comment