There’s very easy and practical way to deal with the need to get a user’s Username and ID when programming in an ASP.NET environment.
Getting the user’s Username is simpler, in the sense that, in order to get a GUID you will need to add both a reference and a “using” statement.
On the other hand, to get the username, you will only need to use an HttpContext request.
Add a reference to: System.DirectoryServices.AccountManagement and a using statement to your code. Having done that, the following piece of code will get the username and ID of the currently logged on user and assign it to local fields.
string currentUserUsername = HttpContext.Current.User.Identity.Name;
Guid currentUserGuid = (Guid)UserPrincipal.Current.Guid;
Do notice that in the case of getting the Guid, you will need to explicitly cast it to Guid.