Showing posts with label login. Show all posts
Showing posts with label login. Show all posts

Tuesday, March 20, 2012

Retrieving Login Name?

Hi,
I need to know how to retrieve the login name of the user into a local
variable so that I can insert it into a table.
I want to do something like this:
DECLARE @.USERNAME varchar(50)
DECLARE @.OTHERFIELD varchar(50)
SET @.USERNAME = EXEC sp_who ?or something like this?
INSERT INTO MYTABLE(Username, OtherField)
VALUES(@.USERNAME, @.OTHERFIELD)
Can anyone show me the correct way to do this?
JDJoe Delphi wrote:
> I need to know how to retrieve the login name of the user into a local
> variable so that I can insert it into a table.
> SET @.USERNAME = EXEC sp_who ?or something like this?
select @.USERNAME = SYSTEM_USER
SYSTEM_USER is a built-in system function. You can look up "system
functions" in the SQL help for some other values as well.

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!