How to get Current User ID in asp.net MVC 4
After some time i finally figured it out ...
the best way to do so is to use the WebSecurty class
var memberId = WebSecurity.GetUserId(User.Identity.Name);
and don't forget to add
[InitializeSimpleMembership]
on top of your controller :)
Written by Nikola Sivkov
Related protips
7 Responses

I think the easiest way is...
var userId = (Guid)Membership.GetUser(User.Identity.Name).ProviderUserKey;
No data attributes needed :)

The easiest way to get the Current User Id in Asp.Net MVC 4 is
WebSecurity.CurrentUserId

Or, just to add to my previous comment, you can also use -
WebSecurity.GetUserId(User.Identity.Name)
Thank you!! this helped me a lot.
I tried these both code
ViewBag.UserId = WebSecurity.CurrentUserId;
ViewBag.UserId = (int)WebSecurity.CurrentUserId;
but I got same error "You must call the "WebSecurity.InitializeDatabaseConnection" method before you call any other method of the "WebSecurity" class. This call should be placed in an _AppStart.cshtml file in the root of your site."
please help, thx
solve my self :
adding [InitializeSimpleMembership] on the top of controller name, and include Filter using MvcMovie.Filters;

Include:
using Microsoft.AspNet.Identity;
Then use extension methods:
User.Identity.GetUserId()
how to implement this code to my controller ?where i should write this code ?