Tuesday, March 20, 2012

Retrieving logged in name for data selection from SQL server

I have a login name being displayed when a user logs in, like so: "Welcome: johndoe23"
This is displayed through the use of the <asp:LoginName> tag.

I want to do a query on the database that only selects data relevant to that userName and wish to
assign that logged in username to a variable so we can use an SQL command similar to:"SELECT something WHERE userID=" + loggedInUserName;

According to MSDN:

By default, theLoginName control displays the name contained in theUser property of thePage class. If theSystem.Web.UI.Page.User.Identity.Name property is empty, the control is not rendered.

**END MSDN**

According to that - the following should work:

sqlStr = "SELECT something WHERE userID=" + Page.User.Identity.Name;

You might need apostrophes - not sure, i use the SqlParameter for everything.

I have never used the built in login features, so give it shot.


|||

We actually progressed to using the guid of the logged in user in order to query the database.

This GUID was a big string of letters. We lost all of this weeks code in a hard drive failure this morning unfortunately and are trying to get back on our feet.

I've found loads about finding the Windows identity of the logged in user, but this is not what we want.

Does anyone know how to retrieve this GUID and assign it to a variable?

Many thanks!

No comments:

Post a Comment