Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows active
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 3:39am
Edited 6-Aug-12 10: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
0 OriginalGriff 6,055
1 DamithSL 4,621
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,500
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 6 Aug 2012
Copyright © CodeProject, 1999-2014
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