aspnet / Identity

[Archived] ASP.NET Core Identity is the membership system for building ASP.NET Core web applications, including membership, login, and user data. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
1.96k stars 870 forks source link

Difference between AddDefaultIdentity and AddIdentity?? #1884

Closed Ponant closed 6 years ago

Ponant commented 6 years ago

In scaffolding Identity I get this in ASP.NET Core 2.1:

  services.AddDefaultIdentity<ApplicationUser>()
                    .AddEntityFrameworkStores<ApplicationDbContext>();

Whereas the previous templates produced this

  services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();

Which was clearer. Is is possible to understand the difference please? What does the former has that the latter does not? Can I just replace the former by the latter? Thanks

HaoK commented 6 years ago

AddDefault uses a different set of apis, but does more or less the same thing except it removes roles functionality by default.

Roles is the only real difference between the two, they both return IdentityBuilders which can further customize identity.

Ponant commented 6 years ago

@HaoK , I am happy to get rid of Roles, except that they seem to be generated in the sql db anyway. 1) 1) Why is that?

2) Could you be somewhat more precise on what you mean by "more or less"?

3) Why is .AddDefaultTokenProviders(); not there anymore?

HaoK commented 6 years ago

You can look at the differences here: https://github.com/aspnet/Identity/blob/c7276ce2f76312ddd7fccad6e399da96b9f6fae1/src/UI/IdentityServiceCollectionUIExtensions.cs#L49

HaoK commented 6 years ago

It doesn't affect the migration, it just doesn't register any role services by default. Its not meant to replace any existing identity code.

HaoK commented 6 years ago

This sugar is mostly for templates to use to collapse DefaultUI/DefaultTokenProviders

Ponant commented 6 years ago

Sorry @HaoK , I did not fully understand your last post. My scenario is that I am not using the defaultUI, so do I need to add AddDefaultTokenProviders to the pippleine if I use AddDefaultIdentity, or is AddDefaultIdentity already taking care of that? I stumbled upon the link you gave before writing this issue and it was not clear enough to me, sorry.

Ponant commented 6 years ago

Oh @HaoK my deepest apologies , I misread the return value of AddDefaultIdentity. It calls AddDefaultTokenProviders.

HaoK commented 6 years ago

All good

Coaden commented 6 years ago

If you still need to use Roles you can do this:

services.AddDefaultIdentity<ApplicationUser>() .AddRoles<ApplicationRole>() .AddEntityFrameworkStores<MyApplicationContext>();