Reputation: 87
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?
Upvotes: 3
Views: 707
Reputation: 15170
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 is System.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 specify HttpContext.Current.User.Identity.Name
.
Upvotes: 2
Reputation: 439
You can pass Controller property "User" to your model. something like this:
public ActionResult Index()
{
IndexModel model = new IndexModel(this.User);
}
Upvotes: 1