Click here to Skip to main content
12,943,528 members (52,873 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
Goodmorning,

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();

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

Solution 1

Use
Environment.UserName
to get the logged username.
  Permalink  
Rate this: bad
 
good
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
        {
            get
            {
                string result = this.Name;
                if (result.LastIndexOf('\\') == -1) return result;
                else return result.Remove(0, result.LastIndexOf('\\') + 1);
            }
        }
 
        public int UserId
        {
            get
            {
                if (userId != -1) return userId;
                return 0;
            }
        }
    }


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

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 4,643
CHill60 2,970
Maciej Los 2,348
Jochen Arndt 1,900
ppolymorphe 1,765


Advertise | Privacy | Mobile
Web02 | 2.8.170518.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