little-aspnetcore-book icon indicating copy to clipboard operation
little-aspnetcore-book copied to clipboard

Compatibility with ASP.NET Core 2.1

Open nbarbettini opened this issue 6 years ago • 13 comments

Placeholder for work to catch the book up with the newest patterns in ASP.NET Core 2.1.

nbarbettini avatar Jun 10 '18 00:06 nbarbettini

Hi,

I worked through the book using ASP.NET Core 2.1.300. Most things worked great. I list the issues that I ran into below and how I worked around them. There might be better ways but I thought this would at least give some pointers to where the book might need to be updated to work well with 2.1.

  • The generated ConfigureServices method in Startup.cs has changed. I found that "ApplicationUser" is not recognized any longer. I was however able to work around this by replacing all instances of "ApplicationUser" with "IdentityUser" in the code.
  • Secondly it seems like the RoleManager is no longer added as a service by default. I had to make a change in ConfigureServices to make this work. Refer to https://stackoverflow.com/questions/50426278/how-to-use-roles-in-asp-net-core-2-1
  • I never got the [Authorize(Roles = "Administrator")] attribute to work properly. I does not let me view the page even when logged in as [email protected]. The UserManager however seems to identify me as having the Administrator role since the "Manage Users" link appears in the nav bar when I log in.
  • In order to be able to run the UnitTest example I had to add the packages Microsoft.AspNetCore.Identity.EntityFrameworkCore and Microsoft.EntityFrameworkCore.InMemory to the unit test project. This might not be a 2.1 issue though, but should be added.
  • In order to be able to run the Integration test example I had to add the packages Microsoft.Extensions.Configuaration.Json and Microsoft.AspNetCore.App.
  • The URI in the integration test had to be changed to include "/Identity", "http://localhost:8888/Identity/Account/Login?ReturnUrl=%2Ftodo"

You can find the code with the modifications described above at my page on GitHub: https://github.com/Backhage/LearnASP.NET

Backhage avatar Jun 10 '18 21:06 Backhage

@Backhage Awesome work! I'd be happy to go through and try the tutorial myself later when I have time to verify what you experienced. I contributed before, then got busy, but I should have time again this weekend and onward.

One thing we'll need to double check is Docker deployment too. The images Microsoft publishes have changed, along with how they recommend using them.

mattwelke avatar Jun 11 '18 14:06 mattwelke

@Backhage To get [Authorize(Roles = "Administrator")] attribute to work properly in 2.1 I had to do the following in Startup.cs:ConfigureServices():

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

(cf. https://github.com/aspnet/Identity/issues/1813)

ZoBoRf avatar Jul 12 '18 22:07 ZoBoRf

@RoBak42 I haven't had a lot of time to work with Identity Framework in ASP.NET Core yet. Never needed auth so far. xD But I do know the industry as a whole is moving from roles to claims. Do you think that issue has to do with roles being deprecated or something out of the box, with the expectation that people use claims? So instead of an Administrator role, we'd use a "HasAdminAccess" claim etc?

mattwelke avatar Jul 12 '18 23:07 mattwelke

@Welkie I don't think roles are deprecated. In ASP.NET Core 2.1 they simply realize that not every application needs roles and separated them. The problem here is to switch the roles back on in the right way. That's what I understand from the discussion in https://stackoverflow.com/questions/50426278/how-to-use-roles-in-asp-net-core-2-1

ZoBoRf avatar Jul 12 '18 23:07 ZoBoRf

For [Authorize(Roles = "Administrator")], you can use as follows:

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

// workaround until asp.net core 2.2
services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, UserClaimsPrincipalFactory<ApplicationUser, IdentityRole>>();

deman avatar Jul 13 '18 03:07 deman

Thanks for the digging @Backhage and @RoBak42!

With ASP.NET Core 2.2 coming, it might make sense to update the book one time for 2.2 and skip 2.1 We'll see how much time I'll have to devote in the next few months.

nbarbettini avatar Aug 05 '18 21:08 nbarbettini

For [Authorize(Roles = "Administrator")], you can use as follows:

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

// workaround until asp.net core 2.2
services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, UserClaimsPrincipalFactory<ApplicationUser, IdentityRole>>();

This works... Thanks

hegdevnayak avatar Sep 21 '18 06:09 hegdevnayak

many thanks to the posters above for their 2.1 compatibility testing. It's greatly appreciated!

I am having an issue with Program.cs for the Creating a Test Administrator Account section. My program.cs lists the 2.1 version code: public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>(); }

Are there any suggestions how I can tweak the book's code to run in the 2.1 world?

ghost avatar Oct 17 '18 15:10 ghost

For [Authorize(Roles = "Administrator")], you can use as follows:

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

// workaround until asp.net core 2.2
services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, UserClaimsPrincipalFactory<ApplicationUser, IdentityRole>>();

To asp.net 2.2, people should replace ApplicationUser with IdentityUser in code or extend it to ApplicationUser.

bingoabs avatar Mar 21 '19 08:03 bingoabs

Looks like this ticket might need to be repurposed to update the book for 3.0 which is launching very soon.

mattwelke avatar Mar 21 '19 14:03 mattwelke

I went with version 2.2 too, but the methods above don't seem to work. UserManager can't recognise the Administrator account either...

Edit: After creating a new DB users with Administrator role, it now works. For anyone else struggling like myself, try with another testAdmin user, or even reset the DB.

mihai80 avatar Apr 19 '19 17:04 mihai80

@Backhage To get [Authorize(Roles = "Administrator")] attribute to work properly in 2.1 I had to do the following in Startup.cs:ConfigureServices():

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

(cf. aspnet/Identity#1813)

Thanky you

shuhratjan avatar Nov 09 '19 06:11 shuhratjan