Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C#
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
Updated 6-Aug-12 9:13am
v2

Use
C#
Environment.UserName
to get the logged username.
 
Share this answer
 
I am using following code :

C#
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.
 
Share this answer
 

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