Problem in getting logged in user Name for ASPX page with (even with this.User.Identity.Name)

Hi  friends,

 I am creating an application that takes current logged in user and displays on the page itself. for this I am getting the user name in PageLoad function by using this.User.Identity.Name and then storing the value in a hidden variable for aspx page. I am using this hidden  variable to display the name of the user. I am using Windows authentication for this.

 Now problem that I am getting is that when multiple user launch the application from different machine and if they click simultaneously, name of one user appears on the paged displayed on the machine of other user.

Say-- user A launch from his maching so he should get "Welcome User A" in his page, and user B launch from his own machine and he should get "Welcome user B". But when both user A and User B launch the application simultaneously ... both User A and B get "Welcome User A".

Please help me out on this, I have tried getting user name with Request.ServerVariables["REMOTE_USER"] as well but of no use Sad. This is a very strange situation.

Ya one more thing. I am using MSAccess in backend for storing some of the user data which also I am accessing.

 

 


---------------------------------------------
Ashwani
---------------------------------------------
100% of the shots you don't take don't go in.
---------------------------------------------
0
ashwani
11/30/2008 2:12:22 PM
asp.net.security 27051 articles. 1 followers. Follow

14 Replies
1309 Views

Similar Articles

[PageSpeed] 25

Hi, are you using a shared function in your code behind by any chance? Can you show your code?


Regards
Smcoxon

No Gem is ever polished without some friction.
0
smcoxon
11/30/2008 11:37:19 PM

Hi, here is the code ...

  public partial class Level_2 : System.Web.UI.Page
    {
        //UNIQUE level code for each page.
        private const string LEVEL_CODE = "VERSION-ONE-LEVEL-TWO";

        private checkAnswer objCheckAnswer;
        private LoadUnloadData objDataRecorder;
        private string strUserName;
        private bool isLevelFreezed = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            //string strLoginUser = this.User.Identity.Name;
            string strLoginUser = Request.ServerVariables["REMOTE_USER"];
            //string strLoginUser = HttpContext.Current.User.Identity.Name;
            if (!strLoginUser.Equals(string.Empty))
            {
                strUserName = strLoginUser.Remove(0, 11);
                objDataRecorder = new LoadUnloadData();

                //Get values of timers
                int[] aryTimerValues = objDataRecorder.loadTimers(strUserName, LEVEL_CODE);

                //Store the values in hidden variable as required by client side page.
                hdnLevelTime.Value = Convert.ToString(aryTimerValues[0]);
                hdnTotalTime.Value = Convert.ToString(aryTimerValues[1]);
                if (aryTimerValues[2] == 1)
                {
                    isLevelFreezed = true;
                    hdnIsLevelFreezed.Value = "1";
                }
                else
                {
                    isLevelFreezed = false;
                    hdnIsLevelFreezed.Value = "0";
                }
                hdnUserName.Value = strUserName;
                hdnLevelID.Value = LEVEL_CODE;
            }
            else
            {
                Response.Redirect("ErrorPage.aspx");
            }
        }


---------------------------------------------
Ashwani
---------------------------------------------
100% of the shots you don't take don't go in.
---------------------------------------------
0
ashwani
12/1/2008 3:21:25 AM

Hi, I cant see any issues with this code but this code isn't the code that reads the logged in username from the hdnUsername hidden field value to display it on an aspx page? Can you show the code that reads the hdnUsername value for display?

The way I approach this is to get the user name when the user logs in using the Login Controls Login1_LoggedIn event and the Membership.GetUser() method. Once I've retrieved the required user information I store it in Session state and retireve it from Session state whenever I need to display the user information on an aspx page.


Regards
Smcoxon

No Gem is ever polished without some friction.
0
smcoxon
12/1/2008 8:03:37 PM

this.User.Identity.Name and then storing the value in a hidden variable for aspx page. I am using this hidden  variable to display the name of the user. I am using Windows authentication for this.

May be i am misunderstanding your question...But

Why are you storing value this.User.Identity.Name in a hiddenVariable ?

 


/GuruBhai
0
guru_sarkar
12/1/2008 9:42:11 PM

My aim is to display windows logged in user name on each page. The user is not shown any login window but the current logged in windows user name only is taken as his identity. What would be best approach for this ? Shall I call this on the very first page and store in state management?? or should I check on each page the name of the user as I am doing now?? 

 But even if I go for any other approach, I would surely want to know the reason of why this is going wrong ... ???

 The code for ASPX page is here

    function window.onload()
    {
        varTotalTime = document.getElementById('hdnTotalTime').value;
        varLvlTime = document.getElementById('hdnLevelTime').value;
        document.getElementById('divUserName').innerHTML = document.getElementById('hdnUserName').value;
        if( document.getElementById('hdnIsLevelFreezed').value == "1")
        {
            document.getElementById('divTimers').style.display = "none";
            document.getElementById('divMessages').style.display = "block";
            document.getElementById('divUserMessage').innerHTML = "<p>You have already cleared this level</p>";
        }
        else
        {
            startTimer();
        }
    }


---------------------------------------------
Ashwani
---------------------------------------------
100% of the shots you don't take don't go in.
---------------------------------------------
0
ashwani
12/2/2008 3:27:08 AM

My aim is to display windows logged in user name on each page.

Just use User.Identity.Name to get the logged-in username which you can get on any page....hidden field is not at all needed

and if you think you don't want to show the user in domainname\username format ... grab the username from the string

yes you can use Session instead of hiddenField

 


/GuruBhai
0
guru_sarkar
12/2/2008 3:26:05 PM

Again your Javascript code for reading the hidden fields looks OK. Going back to your Page_Load event that initially gets the user identity, try reusing the line of code:

string strLoginUser = this.User.Identity.Name; and change it to: strLoginUser = Page.User.Identity.Name; I know 'this' should work but...

Also, I'd start to look at the IIS settings. Take a look at this article, it might help: http://www.eggheadcafe.com/articles/20050703.asp

When user A and user B login simultaneously and user B is displayed as user A, what happens if you refresh user B's aspx page that runs the Page_Load event and code that gets the username? Does it then display correctly?

You should be able to use hidden fields, Session state, User.Identity, Cookie or any other method you chose or create for holding the logged in users identity.


Regards
Smcoxon

No Gem is ever polished without some friction.
0
smcoxon
12/2/2008 10:33:51 PM

Hi Guru,

 The problem is not in the hidden field, and even if I dont use hidden field the problem that I am getting still persists. Please see my first post again to understand my problem. I am able to get the user name on every page by callilng User.identity.name. But when I launch the hosted ASP application from 3-4 different machines and if say 2 users simultaneously opens same page, page on the screens of both the use shows same name that is either of User A or user B.

Please help me if you have any idea what can be the reason....


---------------------------------------------
Ashwani
---------------------------------------------
100% of the shots you don't take don't go in.
---------------------------------------------
0
ashwani
12/3/2008 10:12:25 AM

 Hi Smcoxon,

I made the change as you suggested, from "this"  to "Page" but still having the same problem. Sad. I am looking into the link that you have provided and will let you know is that is of any help for me. But I am on ther verge of smashing my system for this problem ... not able to make out in any way why this is not working.


---------------------------------------------
Ashwani
---------------------------------------------
100% of the shots you don't take don't go in.
---------------------------------------------
0
ashwani
12/3/2008 10:17:28 AM

 Hi,

I am still getting the problem Sad. Tried everything that comes to my mind . . the document also didn't help much ....  still am struck at that point only. I am not able to find out the cause for this problem, cant even trace whats going wrong.


---------------------------------------------
Ashwani
---------------------------------------------
100% of the shots you don't take don't go in.
---------------------------------------------
0
ashwani
12/3/2008 12:48:33 PM

smcoxon:

When user A and user B login simultaneously and user B is displayed as user A, what happens if you refresh user B's aspx page that runs the Page_Load event and code that gets the username? Does it then display correctly?

You should be able to use hidden fields, Session state, User.Identity, Cookie or any other method you chose or create for holding the logged in users identity.

 

Yes on refresh it displays correctly. I tried using Session variable also but still getting that problem. And also I don't want to pass anything in query string.


---------------------------------------------
Ashwani
---------------------------------------------
100% of the shots you don't take don't go in.
---------------------------------------------
0
ashwani
12/3/2008 3:08:36 PM

Hi Ashwani,

This is wierd! The only thing I can think of is that for some reason your IIS server and Windows authentication system is providing your aspx page with the incorrect information as a result of similtaneous logins. This could be a timing, a config setting or slow process. I'm really not sure.

As suggested in my last reply. What happens if you click on the browser refresh for the user with the incorrect information displayed? Does it then show correctly?

 
Regards
Smcoxon

No Gem is ever polished without some friction.
0
smcoxon
12/3/2008 3:10:16 PM

Hi Ashwani,

Take a read through this msdn article on Windows authorisation and Impersonation:

 http://msdn.microsoft.com/en-us/library/system.security.principal.windowsidentity.aspx

http://msdn.microsoft.com/en-us/library/system.security.principal.windowsidentity_members.aspx

http://msdn.microsoft.com/en-us/library/ms998358.aspx

I feel this is the area to look closer at and I think you might need to use the WindowsIdentity Class to get the correct information you need. Hope it helps.

Regards
smcoxon


Regards
Smcoxon

No Gem is ever polished without some friction.
0
smcoxon
12/3/2008 3:24:53 PM

 Thanks Smcoxon,

I'll go through this and will let you know my finding Smile.


---------------------------------------------
Ashwani
---------------------------------------------
100% of the shots you don't take don't go in.
---------------------------------------------
0
ashwani
12/3/2008 3:42:56 PM
Reply:

Similar Artilces:

Separting the User name from the Machine name in User.Identity.Name
string userNameWithMachineName = User.Identity.Name; string userNameWithoutMachineName = System.IO.Path.GetFileName(userNameWithMachineName); HighOnCodingWanna get high! ...

Can not get user name from HttpContext.Current.User.Identity.Name
Hi All; I have tried to use HttpContext.Current.User.Identity.Name to get window user account. However, from debugger, for a long time ago I saw my window user name. But now, I did not see my window user name (from debugger) with the same code and same function call. Please help. Was I doing something wrong? Or any special settings on web.config/machine.config or project to make it work??? I'm stuck Thanks a lot well, you have to have windows integrated authentiction enabled in IIS, IIRC.RTFM - straight talk for web developers. Unmoderated, uncensored, occasionally unre...

What has to happen after login before Page.User.Identity.Name contains the user name?
I am using Membership Services in my new ASP.NET 3.5 app. I want to redirect the user to a page where he can enter various personal details if he has not yet done this. I put some VB.NET code in the LoggedIn event for my Login control that depends on the availability of the current user name in Page.User.Identity.Name but this reference is Nothing at this stage in the process. Page.User.Identity.Name works fine on other pages. When exactly is Page.User.Identity.Name loaded with the name of the current user?David   The authentication process hapen like this... 1-User request a secur...

difference between HttpContext.Current.User.Identity.Name and User.Identity.Name
Q1. what is the difference between 1. HttpContext.Current.User.Identity.Name;and 2. User.Identity.Name Q2.i'm using asp.net 2.0. i'm using the following code to authenticate FormsAuthentication.RedirectFromLoginPage(strEMail, false); Response.Redirect("web/ra.aspx"); my main web.config has <authentication mode="Forms">      <forms          name=".ASPXAUTH"          loginUrl="back.htm"          protection="All" &n...

User.Identity.Name swaps with other logged in users
I seem to be having a problem with User.Identity.Name, never given me any problems in the past, however, i've just put a site live and for some reason, every now and then the value of Name changes to that of another user logged into the system! However, it's only for one page view! Once you view any page again, the information resets back to the original and correct user (i.e. value in .Name). The only one bit of information i could see on google about this related to worker processes recycling.Is this possible? Could the worker process recycling end up with users being confused? I...

Getting the user name of the currently logged in user
I'm writing my first custom module. Just as an experimental project I would just display information about the currently logged in user. I can get the User Id, but I don't know where to get the rest of the information. How do I get this? Hi, Just create a new UserController to get the information about one user via the method GetUser(PortalId, UserId) and store it into a UserInfo. Then you can get the rest of the information from this UserInfo [VB] Dim myUser As New UserInfo Dim myUserController As New UserController myUser = myUserController.GetUser(PortalId, Use...

User.Identity.Name returns email address instead of user name
I have modified the login control so that the user logs in using their email instead of their user name. Here is my code:Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate Dim email As String = Login1.UserNameDim userName As String = Membership.GetUserNameByEmail(Login1.UserName)Dim userInfo As AdditionalUserInfo = New AdditionalUserInfo   If Membership.ValidateUser(userName, Login1.Password) = True ThenDim isFirstTime As Boolean = userInfo.GetUserInfo(userName).IsFirstTime If isFirstTi...

Application User and still getting the logged in User Name
Hi I have a very intranet situation where User opens the Web Page Now here is calls a Database table where User Rights are stored But access to this database table is given to dbo and Application Uer which is created in IIS Server folder by clicking on Allow Anomymous Login=true when I try to run the Table Query and pass the USer Name I get null UserName how to deal with this situation   Thanks       so do you have anonymous access ON? try deny anonymous access to your application. Enable impersonation in your web.config./GuruBhai Hi, By default, ASP.N...

Query logged in user
First, the obliquity I am a newbie to ASP! I have Googled for while now but cannot seem find a solution to my problem. I am trying to display the details of the logged in user (UserName). Below is my code but nothing is displayed by the query. The Label ID="lblIdentity" does display the logged on users username correctly . Any help would be appreciated.   ASPX page…     <asp:Label ID="lblIdentity" runat="server" Style="z-index: 104; left: 14px; position: absolute;             top: 28px" Text="Label">...

Problem with page.user.identity.name
In my application i have a web form named Login.aspx and when i login using the login control it takes me to another page where i want display name of the user as: "Welcome: User" here is the code:LblMsg.Text="Welcome: " +Page.User.Identity.Name;But it show me my computer Name not the user name pls can you rectify this. thank you. This is the place to learn everything on asp.net. I thank to all who help me. If you are using Windows Authentication, it's the expected result. Or if you are talking about Form Authentication and you are using A...

Getting first name and last name from Windows logged in user
 I'm sure there must be an easy way of getting the actual first name and last name from an Windows domain\username via the Windows Identity or Principal object, but I can't seem to figure it out. Does anyone have any examples of this? try this: HttpContext.Current.User.Identity.Name  Thanks - tried that...it returns nothing or domain\loginName :( Please check that on the site properties window (in IIS Admin console), in the security tab, click on the modify or edit button in the authentication and anonymous access group and make sure only the "Windows Inte...

display data for a particular logged in user (User.Identity.Name;)
hi, basically i have a database table with: fileNo, fileName, fileSize, fileType, fileOwner etc at the minute my grid view displays ALL files for every fileOwner, how does one filter this to only displaying the files which belong to a particular fileOwner who is logged in.. i.e. SELECT * FROM tblFile       WHERE fileOwner = (User.Identity.Name)         -   to show files for a user who is logged in   how does one do this please?   thanks and regards Hi,jules U can Do like thisI am assuming that ur ...

Master Page User.Identity.Name problem
Hello I've written some code to check information based on the authenticated user. The following code works on an ASPX page load but fails when I move into a Master Page. Specifically, the error is "Compiler Error Message: BC30451: Name 'User' is not declared."  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ' Get & split Domain user InformationDim strDomainUser As String = User.Identity.Name.... Any help would be appreciated.Thanks,Ed Try:HttpContext.Current.User.identity.nameOrPage.User.I...

Problem in using Page.User.Identity.Name
Hi, I am using this in "feedback" page to get the user id. The authentication type is "windows". I have totally 4 pages "Home","Knowledge","Leisure" and "feedback". Now if i directly open the feedback page then i am able to see the user id of the person in the text box by using Page.User.Identity.Name. But if i first open the other pages and then click on the back button and then click on the "feedback" link to open the feedback page then i am not able to see the user id. When i debugged it Page.User.Identity.Name was returning null string. What is actually the problem. What should i do ...

Web resources about - Problem in getting logged in user Name for ASPX page with (even with this.User.Identity.Name) - asp.net.security

Identity of the first male Muslim - Wikipedia, the free encyclopedia
There is some disagreement among Muslims , and among historians of Islam , as to the identity of the first male convert to Islam after Muhammad ...

Birth certificate mix-up sparks identity fraud fears
Parents of newborn children have been mailed birth certificates for other peoples' babies in a mix-up described as a major privacy breach.

Birth certificate mix-up sparks identity fraud fears
Human error has led to nine parents being sent&nbsp;birth certificates for other peoples' babies, in what has been described&nbsp;as a major ...

'It's time for Aboriginal identity and story to be embraced by all Australians'
Some places in Perth were named after governors or other European elites, wives and family members or places in Britain. Frankly, many names ...

These simple mistakes could compromise your identity on Facebook
... Facebook account. However, Swiped author Adam Levin says there’s one simple mistake many Facebook users make that might compromise their identity ...

Europe has an identity crisis: Eurasia Group - Business Insider Deutschland
Eurasia Group's top-risks report outlines the split between "closed Europe" and "open Europe."

NYPD: 4 tips to keep your money and identity safe
Worried about financial fraud and ID theft? Follow these guidelines courtesy of New York's finest

Lucasfilm Confirms Star Wars: The Force Awakens Stormtrooper TR-8R's Identity
Lucasfilm teased that despite the shot from Chewbacca's almost impossibly powerful bowcaster [...]

Beware Of The Santa Scam: There’s Nothing Holly Jolly About Identity Theft
... best-case scenario involves a scammy site simply taking your money without providing the promised letter, while the worst-case includes identity ...

Israeli Schools Ban Israeli-Arab Romance Novel For ‘Threatening Jewish Identity’
Israeli Schools Ban Israeli-Arab Romance Novel For ‘Threatening Jewish Identity’

Resources last updated: 1/9/2016 2:42:54 AM