Click here to Skip to main content
12,956,173 members (106,848 online)
Rate this:
Please Sign up or sign in to vote.
See more:

with this code source i find the user that i will to have the information from the active directory by the name that i insert in the textbox(in this case txtusername).
i would like to not insert the name manually, but I would like to be taken automatically by the function that returns the name of the user logged into windows.

how can I do? someone could help me please? You can modify the source code please?
this is the code source:
string connection =          ConfigurationManager.ConnectionStrings["ADConnection"].ToString();
            DirectorySearcher dssearch = new DirectorySearcher(connection);
            dssearch.Filter = "(sAMAccountName=" + txtusername.Text + ")";     
            SearchResult sresult = dssearch.FindOne();
            DirectoryEntry dsresult = sresult.GetDirectoryEntry();

            lblmittente_nome.Text = dsresult.Properties["givenName"][0].ToString();
            lblmittente_cognome.Text = dsresult.Properties["sn"][0].ToString();
            lblmail.Text = dsresult.Properties["mail"][0].ToString();
            lbldata.Text = DateTime.Now.ToString("dd MMMM yyyy");
            lblfirma_nome.Text = dsresult.Properties["givenName"][0].ToString();
            lblfirma_cognome.Text = dsresult.Properties["sn"][0].ToString();
            lblfunzione.Text = dsresult.Properties["department"][0].ToString();

Posted 6-Aug-12 2:39am
Updated 6-Aug-12 9:13am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

to get the logged username.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I am using following code :

public class UserIdentity : WindowsIdentity
        private int userId = -1;
        internal FrakoEntitiesConnection context;
        private UserIdentity(WindowsIdentity windowsIdentity)
            : base(windowsIdentity.Token)
        public new static UserIdentity GetCurrent()
            WindowsIdentity identity = WindowsIdentity.GetCurrent();
            return new UserIdentity(identity);
        public string UserName
                string result = this.Name;
                if (result.LastIndexOf('\\') == -1) return result;
                else return result.Remove(0, result.LastIndexOf('\\') + 1);
        public int UserId
                if (userId != -1) return userId;
                return 0;

It's part of an authentication/security system i use.

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,869
CHill60 3,550
Maciej Los 3,243
ppolymorphe 2,070
Jochen Arndt 1,973

Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 6 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100