There are two simple ways to get current user in MVC 5. If you are inside the controller class current user can be fetched as follows,
string userId = User.Identity.GetUserId();
Do not forget to add namespace,
using Microsoft.AspNet.Identity;
Other scenario could be that you are not inside the controller class and want to fetch the user information. You can fetch that using HttpContext class.
HttpContext.Current.User.Identity.GetUserId();
I hope it helps. Happy Coding.
Leave a Reply