2

In old asp.net identity 2.0, there is a CreateIdentityAsync() method in type Microsoft.AspNet.Identity.UserManager. But now asp.net 5 has removed this method, how can I create a ClaimsIdentity representing the user now? Thanks in advance.

public virtual Task<ClaimsIdentity> CreateIdentityAsync(TUser user, string authenticationType);
Tony Woo
  • 198
  • 2
  • 7
  • 1
    This question is essentially a duplicate of http://stackoverflow.com/questions/33514997/usermanager-does-not-contain-a-definition-for-createidentity/33531281#33531281. – Kévin Chalet Nov 16 '15 at 20:30
  • Hi @Pinpoint , thank you. I got this question when using your AspNet.Security.OpenIdConnect.Server. It's a great project. The problem is when I use the method in your answer, please see below. I found the identity.Name is null. Why it is null? How to fix this?Sorry I cannot add the comment in your answer in another question since I'm a new so user. var identity = new ClaimsIdentity(await userManager.GetClaimsAsync(user), OpenIdConnectDefaults.AuthenticationScheme); – Tony Woo Nov 17 '15 at 12:25
  • Hey (and thanks for the kind word!). By default, `ClaimsIdentity.Name` uses the `ClaimTypes.Name` claim, so make sure you explicitly add it since `GetClaimsAsync` will unlikely add it for you (it basically only returns custom claims). When using `UserClaimsPrincipalFactory`, it's automatically done for you by Identity: https://github.com/aspnet/Identity/blob/dev/src/Microsoft.AspNet.Identity/UserClaimsPrincipalFactory.cs#L92. – Kévin Chalet Nov 17 '15 at 15:32
  • Hi @Pinpoint. yes the 'UserClaimsPrincipalFacotry' works, it can get the 'ClaimsIdentity' now. But on the OAtuh client('UseJwtBearerAuthentication') after authenticated the user, the identity.Name is still null.. Can you help me? – Tony Woo Nov 18 '15 at 06:31
  • Don't forget that `AspNet.Security.OpenIdConnect.Server` will refuse to serialize the claims that don't explicitly specify a destination, to avoid leaking confidential data in potentially readable tokens. You can find more information on this SO question: http://stackoverflow.com/questions/32299131/using-claims-with-openidconnect-server-in-asp-net-5/32299781. If you need a code sample demonstrating how you can specify an existing principal, don't hesitate to open a separate question and I'll post a complete snippet. – Kévin Chalet Nov 18 '15 at 11:57
  • Hi @Pinpoint, I asked a new question here http://stackoverflow.com/questions/33797838/how-to-specify-the-destination-for-an-existing-claimsidentity , can you help me? Thanks. – Tony Woo Nov 19 '15 at 07:42

3 Answers3

1

Is this what you are looking for?

http://aspnet-docs-example.readthedocs.org/en/latest/autoapi/Microsoft/AspNet/Identity/IUserClaimsPrincipalFactory-TUser/

IUserClaimsPrincipalFactory Interface

Provides an abstraction for a factory to create a System.Security.Claims.ClaimsPrincipal from a user.

Andreas
  • 1,355
  • 9
  • 15
0

This has been changed to UserManager.CreateAsync and returns an IdentityResult indicating whether or not the new user was successfully created.

If you want to add Claims, you can do this via UserManager.AddClaimAsync.

cygnim
  • 1,985
  • 2
  • 16
  • 22
0

Here you can find how to work with ClaimIdentity example with a sample downloadable project

Moumit
  • 8,314
  • 9
  • 55
  • 59