简体   繁体   中英

how can I get the current user's id (In an asp.net/c# website)?

In an asp.net/c# website where I use form authentication, I'd like to relate a certain type of content with the user who created it.

For this I'd like to access the current logged in user's id, so that i can pass it to the database as a foreign key.

There is an id column in the "my_aspnet_users" table that vs auto creates, but I could not find it in the User or Profile object's properties.

how can I access this property??

You'll need to query your database for any extra user info you need. Forms authentication doesn't know anything about user id's, or whatever primary key you use for your user table.

Presumably you're storing the username in the cookie, which is available to you in User.Identity.Name. Just look up the user in the database by username. You could also store the user id in the authentication cookie.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM