Identity: Difference between AddDefaultIdentity and AddIdentity??

Created on 19 Jul 2018  路  9Comments  路  Source: aspnet/Identity

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

discussion

Most helpful comment

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

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

All 9 comments

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.

@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?

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.

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

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.

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

All good

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

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

Was this page helpful?
0 / 5 - 0 ratings