2017-03-05 1 views
3

Dans un contrôleur principal ASP.NET, je peux accéder à User pour obtenir toutes les revendications d'utilisateur en cours comme ClaimsPrincipal. Maintenant, j'ai ajouté un nouvel utilisateur en utilisant userManager.CreateAsync, ajouté quelques revendications et besoin de toutes ses revendications en tant que ClaimsPrincipal pour une autre fonction.
J'ai essayé les suivantes:Obtenir ClaimsPrincipal à partir de usermanager

var user1 = new IdentityUserEntity 
    { 
// set username and stuff 
    }; 
    var result = await userManager.CreateAsync(user1, passwort); 
    await userManager.AddClaimAsync(user1, new System.Security.Claims.Claim(MyClaimTypes.Stuff, MyClaimValues.Whatever)); 

après, je créé un ClaimsPrincipal comme ceci:

var user1cp = new ClaimsPrincipal(new ClaimsIdentity(await userManager.GetClaimsAsync(user1))); 

Le problème est, ce ClaimsPrincipal ne contient pas le NameIdentifier (et peut-être d'autres choses?).

Comment obtenir un ClaimsPrincipal complet d'un utilisateur que j'ai récemment ajouté à la base de données?

Répondre

2

Je pense que vous êtes à la recherche pour la méthode CreateUserPrincipalAsync de SignInManager:

/// <summary> 
/// Creates a <see cref="ClaimsPrincipal"/> for the specified <paramref name="user"/>, as an asynchronous operation. 
/// </summary> 
/// <param name="user">The user to create a <see cref="ClaimsPrincipal"/> for.</param> 
/// <returns>The task object representing the asynchronous operation, containing the ClaimsPrincipal for the specified user.</returns> 
public virtual async Task<ClaimsPrincipal> CreateUserPrincipalAsync(TUser user) => await ClaimsFactory.CreateAsync(user);