bigyeti bigyeti - 1 year ago 55
ASP.NET (C#) Question

Why can I not get the UserID with Identity?

In my webforms application, I've taken the Login function from the Login page in the accounts folder, and inserted it into Site.Master.cs so people can login from a drop down form on any page. I want to track logins, so I wanted to take the userid of the person logging in and insert it into a table with a datestamp.
However, with this line of code

string thisuser = User.Identity.GetUserId().ToString();

I'm getting a red line under 'Identity' and the tooltip says "User does not contain a definition for Identity" even though I've included the namespace

using Microsoft.AspNet.Identity;

The frustrating thing is that this works fine elsewhere in a different page.

Answer Source

You should try using System.Web.HttpContext.Current.User.Identity.GetUserId();