2017-03-05 4 views
3

In einem ASP.NET Core Controller kann ich auf User zugreifen, um alle aktuellen Benutzeransprüche als ClaimsPrincipal zu erhalten. Jetzt habe ich einen neuen Benutzer mit userManager.CreateAsync hinzugefügt, einige Ansprüche hinzugefügt und alle Ansprüche als ClaimsPrincipal für eine andere Funktion benötigt.
Ich habe versucht, die folgenden:Get ClaimsPrincipal von 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)); 

danach habe ich eine ClaimsPrincipal wie folgt aus:

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

Problem ist, diese ClaimsPrincipal nicht die Nameidentifier enthält (und vielleicht auch andere Sachen?).

Wie erhalte ich ein vollständiges ClaimsPrincipal von einem Benutzer, den ich der Datenbank hinzugefügt habe?

Antwort

2

Ich glaube, Sie sind für CreateUserPrincipalAsync Methode von SignInManager suchen:

/// <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); 
Verwandte Themen