Query logged in user - using User.Identity.Name

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">

           

            </asp:Label>

       

        <asp:LoginName ID="LoginName1" runat="server" Style="z-index: 100; left: 52px; position: absolute;

            top: 4px" />

        <asp:LoginStatus ID="LoginStatus1" runat="server" Style="z-index: 101; left: 0px;

            position: absolute; top: 0px" />

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

            DataSourceID="SqlDataSource1" Style="z-index: 102; left: 10px; position: absolute;

            top: 157px">

        </asp:GridView>

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ASPNETDBConnectionString %>"

            SelectCommand="SELECT * FROM [vw_aspnet_Users] WHERE ([UserName] = @UserName)">

            <SelectParameters>

                <asp:ControlParameter ControlID="lblIdentity" Name="UserName" PropertyName="Text"

                    Type="String" />

            </SelectParameters>

        </asp:SqlDataSource>

 

CS page..

 

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

 

public partial class home : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if (User.Identity.IsAuthenticated)

        {

            lblIdentity.Text = "The current user is " + User.Identity.Name;

        }

        else

        {

            lblIdentity.Text = "The current user is not authenticated.";

        }

 

    }

}

 

 

0
telboy
2/23/2007 11:23:49 PM
asp.net.security 27051 articles. 1 followers. Follow

12 Replies
874 Views

Similar Articles

[PageSpeed] 56

Hi,

try to set on authentication mode to windows in your web.config, e. g.:

<system.web>
  <authentication mode="Windows"/>
  ...

Regards
Marc André

0
Zhou
2/24/2007 7:01:11 AM

Hi Marc,

Thanks for your reply. I have tried this but have the same problem. The logged on username is diplayed on the page but nothing is returned by the sql query.

0
telboy
2/24/2007 10:12:42 AM
You are setting the Text property of the lblIdentity control to more than just the username. The value of that Text property is used as a parameter for the WHERE in your select command, which will not match the UserName column value.
0
PaulGarner
2/24/2007 10:31:30 AM
Could you be a little more precise what I need to change....
0
telboy
2/24/2007 11:19:13 AM

Hi,

he means: Your label Text contains the following text: "The current user is USERNAME", so the where part contains still this text like this:

Where (Username = 'The current user is USERNAME')

Try to set the only the username to the label: 

lblIdentity.Text = "The current user is " + User.Identity.Name;

Regards
Marc Andre

0
Zhou
2/24/2007 11:47:20 AM

Hi,

he means: Your label Text contains the following text: "The current user is USERNAME", so the where part contains still this text like this:

Where (Username = 'The current user is USERNAME')

Try to set the only the username to the label: 

lblIdentity.Text = User.Identity.Name;

Regards
Marc Andre

0
Zhou
2/24/2007 11:47:39 AM
You are using the Text property of  lblIdentity to locate the row, so what you are sending to SQL Server is a query like this: "SELECT * FROM [vw_aspnet_Users] WHERE ([UserName] = 'The current user is X')". I think you need an additional label for the "'The current user is" text and only have lblIdentity,Text set to User.Identity.Name. Hope that helps.
0
PaulGarner
2/24/2007 12:04:14 PM

Thanks Marc,

I have tried this but still no result from the query. I appreciate your help.

Terry

 

0
telboy
2/24/2007 12:13:19 PM
I put together a test and it worked fine. Could you post your modified aspx and cs files please Terry.
0
PaulGarner
2/24/2007 12:35:17 PM

Hi,

the property AutoGenerateColumns is set to "False" set this property to AutoGenerateColumns="True" and you see your results as expected ... i hope.

Regards
Marc André

0
Zhou
2/24/2007 12:35:20 PM
Well spotted, Marc!
0
PaulGarner
2/24/2007 12:42:04 PM

This works now – well spotted. Thanks to both of you, I really do appreciate your help!

 

Terry

0
telboy
2/24/2007 12:53:28 PM
Reply:

Similar Artilces:

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...

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 ot...

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...

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! ...

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 ...

How secure passing user credentials like user name, password values in query string to httphandler
Hi,  How secure passing user credentials like user name, password values in query string to httphandler like login.ashx?uid=xxx&pwd=yyy  Surendra SurendraIf this helps mark it as answer I believe it's very insecure. Anyone who is smart enough can get the credentials from the querystring directly.Thanks,MaxLet Me Google That For You!  Have a look at Create a querystring that is encrypted and expires at http://www.codeproject.com/KB/dotnet/EncryptionAndQueryStrings.aspxWhilst I would not recommend any design that passed user name and password in the query strin...

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...

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...

User.Identity.Name in User Control
Can I use User.Identity.Name in a User Control (.ascx file). I tried to use it but got the following error: " The type or namespace name 'User' could not be found (are you missing a using directive or an assembly reference?) " regards Try using HttpContext.User.Identity.Name HTH... Chris...

User.Identity.Name in User Control
What namespace do I have to include in my User Control code behind to be able to access the Forms Login User.Identity.Name information? Thanks!I haven't failed, I've found 10,000 ways that don't work System and System.Web.UI. Here's a sample of a user control retrieving User.Identity.Name: using System; using System.Web.UI; public class MyClass: Page { public void Page_Load(Object Source, EventArgs e) { Response.Write("User.Identity.Name=" + User.Identity.Name); } } Thanks lauraThis posting is provided "AS IS"...

User Control
I am trying to convert my aspx page that has all my graphics into a header user control, in the page I reference the username that is logged into the site.  I am using the User.Identity.Name function but it tells me that User is not identifited.  This worked just fine before I moved it to the ascx page. Anyone have any ideas? Thanks Craig In a user control or custom control your will need to use either:Page.User.Identity.Name orHttpContent.Current.User.Identity.Name If using the latter, don't forget to import the System.Web namespaceBill, WESNet Designs...

User.Identity.Name and Web User Control,
I am trying to user User.Identity.Name.ToString in a WebUser Control but it doesnt seem to work. Is their any way around this its tells me "User.Identity " is undeclared. Here is the code that was working when the form was still a web form.   CurrentRow.Item(5) = User.Identity.Name.ToString EmployeeDataTable.GetChanges() EmployeesAdapter.Update(CurrentRow)  It think it needs an import stsaement at the top can you help? Thnks Sanson Use System.Web.HttpContext.Current.UserThanks, EdMicrosoft MVP - ASP/ASP.NET thank you! Thanks for the advice. I couldnt actual...

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 is authenticated, but User.Identity.Name is blank!?
Anyone know what might cause such a situation?  This has been working for over a year as I work on a small site I've been working on.  It stopped working sometime soon and there is only a few things I can think of that might have something to do with it. I enabled Roles.  For a sub-directory that I wanted only admins to be able to access. I installed VS 2008 Team System.  I did not upgrade my website to a new .Net version  Here are the relevant sections of my web.config file:<roleManager enabled="true" /><membership defaultProvider=&qu...

Web resources about - Query logged in user - using User.Identity.Name - asp.net.security

Sexual orientation and gender identity at the United Nations - Wikipedia, the free encyclopedia
... its founding in 1945, the United Nations had not discussed LGBT rights (regarding equality regardless of sexual orientation or gender identity ...

Finger reveals Paris terrorist's identity - AdelaideNow Search Search
FRENCH police have identified the first of seven gunmen who killed at least 129 people in a wave of carnage claimed by the Islamic State group, ...

Wheatbelt man whose identity was stolen by scammer fears he'll strike again
A Wheatbelt man fears he knows the person who stole his identity to try and scam backpackers - and may strike again.

Finger reveals Paris terrorist's identity - HeraldSun Search Search
FRENCH police have identified the first of seven gunmen who killed at least 129 people in a wave of carnage claimed by the Islamic State group, ...

Maroubra baby: still no identity for baby buried in sandy grave who could have been up to two weeks old ...
Almost a year since kids playing in the sand after a Nippers carnival found the decomposing&nbsp;remains of an infant, police are frustratingly ...

Android's Photo App Reveals the Identity of El Gato Loco
Every weekday, we bring you the Ad Age/iSpot Hot Spots, new and trending TV commercials tracked by iSpot.tv, a company that catalogs, tags and ...

VTech Learning Lodge hacked! Your child's identity and innocence is at risk
... If you ask me, those things are indeed personal identifiers. Not only can bad guys use this information in nefarious ways regarding identity, ...

New ‘Batman v. Superman’ teaser trailer reveals the identity of Gotham’s knight
We all know who Batman really is: he’s Bruce Wayne, part of Gotham’s elite and one of the richest people in the city. But most people in Gotham ...

Be Careful With Political Scorecards And One-Issue And Identity Politics Groups
... in Congress and reactionary Illinois Blue Dog Cheri Bustos. It's all about the criteria or criterion. If all that matters is simplistic identity ...

Taye Diggs: I want my son to choose his racial identity
The actor says his son has the right to identify as white just as much as he does to identify as black

Resources last updated: 12/3/2015 7:36:50 AM