Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#Windowsactive
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
Edited 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
0 Sergey Alexandrovich Kryukov 459
1 OriginalGriff 441
2 sanket saxena 349
3 Tadit Dash 310
4 Abhinav S 235
0 Sergey Alexandrovich Kryukov 12,109
1 OriginalGriff 7,326
2 Peter Leow 5,013
3 Abhinav S 4,013
4 Maciej Los 3,575


Advertise | Privacy | Mobile
Web03 | 2.8.140421.2 | Last Updated 6 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid