I want to fetch the current logged in username for a databasequery in C# and trying to find a C# version of the ASP “command” User.Identity.Name. When I’m googling it I only find people that’s fetching the Windows username with Environment.Username and similiar solutions – which is not what I need.
So, I’m speaking of the accounts made from the ASP Configuration dialog in VS, or the auto-generated register functions. I can register/login/logout and everything, so I’m guessing there is some sort of session that’s storing the info I need. I just don’t know where I can find it.
I hope I’m not that confusing 🙂 Anyone know how to fetch my session-info from a C# model?
You can get the name of the current user in MVC the same way as you can in classic ASP, with
User.Identity.Name. The whole namespace isSystem.Web.HttpContext.Current.User.Identity.Name.Without seeing your code I can’t be sure, but I imagine you aren’t including
System.Web, and even if you are you may need to specifyHttpContext.Current.User.Identity.Name.