none
how to get current login user all active directory details in c# asp.net core RRS feed

  • Question

  • Hi 

    i have an application which runs in asp.net core angular framework hosted in iis with windows authentication

    need all active directory details when any user login to the application with windows authentication

    i tried different ways but i'm getting appool username instead of login user name



    Friday, February 15, 2019 5:29 PM

All replies

  • Hi sarandoniparthi,

    Thank you for posting here.

    For your question, a simple code not on asp.net for your reference.

            public static void GetAD(string samAccountName)
            {         
                using (var context = new PrincipalContext(ContextType.Domain))
                {
                    using (UserPrincipal user = new UserPrincipal(context))
                    {
                        user.SamAccountName = samAccountName;
                        using (var searcher = new PrincipalSearcher(user))
                        {
                            foreach (var result in searcher.FindAll())
                            {
                                Console.WriteLine(result.Name);
                              //get the details here of your current account.
    
                            }
                        }
                    }
    
                }
            }

    Invoke with the way below.

                string samAccountName = Environment.GetEnvironmentVariable("USERNAME");
                GetAD(samAccountName);


    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.




    Monday, February 18, 2019 2:32 AM
  • Hi,

    In the server i'm able to get the name but when i host that in iis while browsing the site from out of teh server i'm not getting anything. 

    Monday, February 18, 2019 5:34 AM
  • Hi sarandoniparthi,

    For desktop application, the code works well.

    If you need further help with ASP.NET, you could post a new thread in ASP.NET forum.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 18, 2019 5:37 AM