I am testing a few things here in a basic webpage, and I noticed something very odd. I’m not sure if this behavior is to be expected, but it does make me wonder…
I know I can get the Current User ID of the person currently logged in, like so:
@WebSecurity.CurrentUserId
And just to see how Sessions are used, I thought I’d just store (as an example) the CurrentUserId in a Session variable once the user logs in, like this:
@Session["UserIDthing"] = @WebSecurity.CurrentUserId;
And then on another page, I just output the session id (which should be exactly equal to 1, because thats what my UserId is), like this:
@Session["UserIDthing"]
But, instead of it outputting “1”, it outputs minus 1 “-1”. Why does this happen?

And just to make sure I was right about the user id, I outputted the user id using:
@WebSecurity.CurrentUserId
And it displayed the correct ID, which is just “1”
It probably has nothing to do with
Session. Try to store the@WebSecurity.CurrentUserIdsome other way to ensure that theCurrentUserIdhas actually been set after the user logs in (maybe by logging it to a file or use a static variable for testing).