J'essaie de trouver un document ou un exemple de la manière dont vous ajouteriez des revendications personnalisées à l'identité de l'utilisateur dans MVC 5 à l'aide de l'identité ASP.NET. L'exemple devrait indiquer où insérer les revendications dans le pipeline de sécurité OWIN et comment les conserver dans un cookie à l'aide de l'authentification par formulaires.
Peut-être que le article suivant peut aider:
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);
Le bon endroit pour ajouter des revendications, en supposant que vous utilisez le modèle de projet ASP.NET MVC 5 est dans ApplicationUser.cs
. Il suffit de rechercher Add custom user claims here
. Cela vous mènera à la méthode GenerateUserIdentityAsync
. Il s'agit de la méthode appelée lorsque le système ASP.NET Identity a récupéré un objet ApplicationUser et doit le transformer en un ClaimsIdentity. Vous verrez cette ligne de code:
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
Après c'est le commentaire:
// Add custom user claims here
Et enfin, il retourne l'identité:
return userIdentity;
Donc, si vous voulez ajouter une revendication personnalisée, votre GenerateUserIdentityAsync
pourrait ressembler à quelque chose comme:
// 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;
Si vous souhaitez ajouter des revendications personnalisées au moment de l'enregistrement, ce code fonctionnera:
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
vous pouvez effectuer les opérations suivantes dans WEB API C #
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
foreach(var Rol in roles)
{
identity.AddClaim(new Claim(ClaimTypes.Role, Rol));
}
identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
identity.AddClaim(new Claim(ClaimTypes.Email, user.Correo));
identity.AddClaim(new Claim(ClaimTypes.MobilePhone, user.Celular));
identity.AddClaim(new Claim("FullName", user.FullName));
identity.AddClaim(new Claim("Empresa", user.Empresa));
identity.AddClaim(new Claim("ConnectionStringsName", user.ConnectionStringsName));
....