tutorial net custom asp asp.net-mvc-5 asp.net-identity

asp.net mvc 5 - custom - Cómo agregar reclamos en ASP.NET Identity



asp net identity custom roles (3)

El lugar correcto para agregar reclamos, suponiendo que está utilizando la plantilla del proyecto ASP.NET MVC 5, está en ApplicationUser.cs . Simplemente busque Add custom user claims here . Esto lo llevará al método GenerateUserIdentityAsync . Este es el método que se llama cuando el sistema de identidad ASP.NET ha recuperado un objeto ApplicationUser y necesita convertirlo en una ClaimsIdentity. Verás esta línea de código:

// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

Después de eso es el comentario:

// Add custom user claims here

Y finalmente, devuelve la identidad:

return userIdentity;

Por lo tanto, si desea agregar un reclamo personalizado, su GenerateUserIdentityAsync podría ser algo así como:

// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); // Add custom user claims here userIdentity.AddClaim(new Claim("myCustomClaim", "value of claim")); return userIdentity;

Estoy tratando de encontrar un documento o ejemplo de cómo agregaría reclamos personalizados a la identidad del usuario en MVC 5 utilizando ASP.NET Identity. El ejemplo debe mostrar dónde insertar las reclamaciones en la canalización de seguridad de OWIN y cómo persistirlas en una cookie mediante la autenticación de formularios.


Quizás el siguiente artículo puede ayudar:

var claims = new List<Claim>(); claims.Add(new Claim(ClaimTypes.Name, "Brock")); claims.Add(new Claim(ClaimTypes.Email, "[email protected]")); var id = new ClaimsIdentity(claims,DefaultAuthenticationTypes.ApplicationCookie); var ctx = Request.GetOwinContext(); var authenticationManager = ctx.Authentication; authenticationManager.SignIn(id);


Si desea agregar reclamos personalizados en el momento de la inscripción, este código funcionará:

var user = new ApplicationUser { UserName = model.UserName, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); // Associate the role with the new user await UserManager.AddToRoleAsync(user.Id, model.UserRole); // Create customized claim await UserManager.AddClaimAsync(user.Id, new Claim("newCustomClaim", "claimValue")); if (result.Succeeded) {...etc