net mvc framework example crear asp and c# asp.net-mvc entity-framework asp.net-identity seed

framework - login mvc asp net c#



Cómo crear SecurityStamp para AspNetUser en ASP.NET MVC 5 (1)

El sello de seguridad puede ser lo que quieras. A menudo se confunde ser una marca de tiempo, pero no lo es. Se anulará la identidad de ASP.NET si algo cambia en la entidad del usuario. Si está trabajando en el contexto directamente, la mejor manera sería generar un Guid nuevo y usarlo como sello. Aquí hay un ejemplo simple:

var users = new List<ApplicationUser> { new ApplicationUser { PasswordHash = hasher.HashPassword("TestPass44!"), Email = "[email protected]", UserName = "[email protected]", SecurityStamp = Guid.NewGuid().ToString() }, new ApplicationUser { PasswordHash = hasher.HashPassword("TestPass44!"), Email = "[email protected]", UserName = "[email protected]", SecurityStamp = Guid.NewGuid().ToString() } };

Cuando creo un usuario por acción Registrar cuando la aplicación está ejecutando, el usuario de la aplicación obtiene SecurityStamp. Cuando agrego usuario por:

if (!context.Users.Any()) { System.Diagnostics.Debug.WriteLine("INSIDE"); var hasher = new PasswordHasher(); try { var users = new List<ApplicationUser> { new ApplicationUser{PasswordHash = hasher.HashPassword("TestPass44!"), Email = "[email protected]", UserName = "[email protected]"}, new ApplicationUser{PasswordHash = hasher.HashPassword("TestPass44!"), Email = "[email protected]", UserName = "[email protected]"} }; users.ForEach(user => context.Users.AddOrUpdate(user)); context.SaveChanges(); } catch (DbEntityValidationException e) { System.Diagnostics.Debug.WriteLine("EXC: "); foreach (DbEntityValidationResult result in e.EntityValidationErrors) { foreach (DbValidationError error in result.ValidationErrors) { System.Diagnostics.Debug.WriteLine(error.ErrorMessage); } } } }

El usuario no obtiene el sello de seguridad:

y luego cuando quiero iniciar sesión me sale:

Pregunta: ¿Cómo generar SecurityStamp para el usuario?