which will always return -1, you need the code below
int currentuserid = WebSecurity.GetUserId(username);
You can then verify that the user ID above matches the user ID in the model to prevent users from changing the code of other users.
as an extra. I use this in my base controller:
public int GetUserId() { var userid = "0"; if (Request.IsAuthenticated && User.Identity.Name != null) { var membershipUser = Membership.GetUser(User.Identity.Name); if (membershipUser != null) { if (membershipUser.ProviderUserKey != null) { userid = membershipUser.ProviderUserKey.ToString(); } } } return Convert.ToInt32(userid); }
source share