locked
Get Username RRS feed

  • Question

  • User-957910121 posted

    I have an application that uses Windows Authentication on my company network, this applications  containts a particular formview form that contains a dropdown list element. I want to populate this with the username of the person who is using the application but I am un sure on how to set the datasource of the control to talk to the windows authnetication tier and get the username.

    Anyone have any ideas on how this can be done?

    Wednesday, October 13, 2010 9:36 AM

Answers

  • User915387828 posted

    Hi Yanayaya,

    Getting the User Name :

    using three ways we can get the User Name using C#

    1) System.Security.Principal.WindowsPrincipal p = System.Threading.Thread.CurrentPrincipal as System.Security.Principal.WindowsPrincipal;

    string strName = p.Identity.Name;

    [ OR ]

    2) string strName = HttpContext.Current.User.Identity.Name.ToString();

    [ OR ]

    3) string strName = Request.ServerVariables["AUTH_USER"]; //Finding with name

    string strName = Request.ServerVariables[5]; //Finding with index

    In Above 3 Cases returnin string contains DomainName\WinNTLoggedUserName

    (for Ex: Microsoft\Bill.Gates. Here Microsoft is domain Bill.Gates is Logger User Name )

    Using string operations seperate the DomainName and UserName.

    Please check the following link:

    http://www.codeproject.com/KB/aspnet/How_to_NT_User_Name.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 18, 2010 3:50 AM

All replies

  • User1224194097 posted

    I want to populate this with the username of the person who is using the application but I am un sure on how to set the datasource of the control to talk to the windows authnetication tier and get the username.

    If you are using WindowsAuthentication, you can use Environment.UserName and that should give username whoever logged in.

    Wednesday, October 13, 2010 11:50 AM
  • User-957910121 posted

    Can you get any other information returned like the email address and such like?? 

    Thursday, October 14, 2010 4:27 AM
  • User915387828 posted

    Hi Yanayaya,

    Getting the User Name :

    using three ways we can get the User Name using C#

    1) System.Security.Principal.WindowsPrincipal p = System.Threading.Thread.CurrentPrincipal as System.Security.Principal.WindowsPrincipal;

    string strName = p.Identity.Name;

    [ OR ]

    2) string strName = HttpContext.Current.User.Identity.Name.ToString();

    [ OR ]

    3) string strName = Request.ServerVariables["AUTH_USER"]; //Finding with name

    string strName = Request.ServerVariables[5]; //Finding with index

    In Above 3 Cases returnin string contains DomainName\WinNTLoggedUserName

    (for Ex: Microsoft\Bill.Gates. Here Microsoft is domain Bill.Gates is Logger User Name )

    Using string operations seperate the DomainName and UserName.

    Please check the following link:

    http://www.codeproject.com/KB/aspnet/How_to_NT_User_Name.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 18, 2010 3:50 AM