we are using asp.net core and in we are unable to get user id directly so here is working code
var UserID = this.User.Identities.FirstOrDefault()?.Claims?.ToList().Where(x => x.Type == "MembershipId").SingleOrDefault()?.Value;
if there is any way to get userId directly from User.Identity let me know.