Could not get logon user name

Last post 06-05-2008 10:45 AM by mjtdaues. 7 replies.

Sort Posts:

  • Could not get logon user name

    05-15-2008, 1:12 AM

    Hi Dear All,

     I have been working for 2 days trying to figure out the problem with this line of code. I could not get/retrieve the logon user name using "HttpContext.Current.Request.ServerVariables["LOGON_USER"];" and somewhere I got this suggestion "System.Threading.Thread.CurrentPrincipal.Identity.Name;" which still does not solve my problem. I am using VS 2005 and WinXP SP-2 and .Net Framework 2.0. Below is the two lines on which I was trying to get/retrieve the user name but always get blank/empty string.
     

    1. string user = HttpContext.Current.Request.ServerVariables["LOGON_USER"]; //Does not work

    2. string user = System.Threading.Thread.CurrentPrincipal.Identity.Name; //Does not work either.

     So, I badly need some help if some of you could bring me out of this hell.

     

    Thanks
     

  • Re: Could not get logon user name

    05-15-2008, 1:42 AM
    • Loading...
    • kunal1982
    • Joined on 04-01-2008, 6:19 AM
    • India
    • Posts 9

    CurrentPrincipal.Identity.Name is used to pick up the LoginID of the user who logged using windows digest authentication [ from active directory].

    Are you using Forms authentication or windows authentication? 

     

     

     

  • Re: Could not get logon user name

    05-15-2008, 2:15 AM

    I am using windows authentication in my web.config file as follow:

    <authentication mode="Windows" /> 

     I enabled Anonymous user from IIS and enabled Integrated Windows Authentication, when i disabled Anonymous user I was prompt to enter user name and password with windows logon dialog box, and giving my user name and password does not help, instead i got that i am not authorised to view the page.

    Actually my boss wanted the Anonymous  user should be disabled...

     

    My code behind in C# is as follow:

    1    public static string GetCurrentUsername() 
    2 {
    3 string user = System.Security.Principal.WindowsIdentity.GetCurrent().Name;//It does not assign value to variable user
    4 //string user = HttpContext.Current.Request.ServerVariables["LOGON_USER"];//This does not work either
    5 int domainIndex = user.IndexOf(@"\");
    6 if (domainIndex != -1)
    7 return user.Substring(domainIndex + 1);
    8 return user;
    9 }
     

     

    Thanks 

  • Re: Could not get logon user name

    05-15-2008, 4:27 AM
    • Loading...
    • kunal1982
    • Joined on 04-01-2008, 6:19 AM
    • India
    • Posts 9

    Check for the settings in IIS. Right click your site name in IIS. Go on Directory SecurityTab and enable Basic authentication checkbox. Do select the default domain and realm.

    Remember that Anoymous access checkbox, digest authentication , integrated windows  authentication checkbox should be unchecked.

  • Re: Could not get logon user name

    05-17-2008, 6:33 AM

    Dear,

    Actually I am using Windows XP - SP2 .  My friends who are working on the same project using the same settings with same OS does not have any problem by disabling Anonymous user from their IIS settings. The problem is that when I change my IIS settings exactly same with my friends' settings I got the login/authorisation problem but my friends does not get. This made me suspect that there is some problem in my OS settings related to web application (IIS, ASP.Net and related applications).

    I badly need help, what can i do, please friends help me.

     

    Regards,


     

  • Re: Could not get logon user name

    05-21-2008, 3:51 AM
    Answer

    Hi

    You can get answer from this thread,

    http://forums.asp.net/t/1260883.aspx

     Hope it helps

    Best Regards
    XiaoYong Dai
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
  • Re: Could not get logon user name

    05-21-2008, 7:17 AM
    Answer
    • Loading...
    • Deleo
    • Joined on 11-04-2007, 10:33 AM
    • Posts 135

    If you are inside a page you could just get the user by calling Page.User.Identity.Name, but you have to remember that if you enable anonymous user, then this is what IIS is using by default. The anonymous user is the account you set up in IIS manager and is by default Network service (I think).

    Once you perform a windows login or form, you should get the correct user logged in. If you retrieve account by the enviroment variable, you will get the system user account i think...not sure there. Use the Page.User.Identity to retrieve the ASP user currently logged on to the page.

     

  • Re: Could not get logon user name

    06-05-2008, 10:45 AM
    • Loading...
    • mjtdaues
    • Joined on 05-23-2008, 2:08 AM
    • Posts 4

    For this you need to make the configuration changes in the IIS.

    Go to IIS right click on the site, Go to "Directory Security" Tab.    Click on Edit button from  "Anonimous Access and Athuntication Control" section.

    Now uncheck anononimous access and check on "integrated windows authentication".

     

     

     

     

Page 1 of 1 (8 items)