Open nbarbettini opened 6 years ago
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.
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.Microsoft.Extensions.Configuaration.Json
and Microsoft.AspNetCore.App
.You can find the code with the modifications described above at my page on GitHub: https://github.com/Backhage/LearnASP.NET
@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.
@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();
@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?
@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
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>>();
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.
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
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?
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
.
Looks like this ticket might need to be repurposed to update the book for 3.0 which is launching very soon.
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.
@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
Placeholder for work to catch the book up with the newest patterns in ASP.NET Core 2.1.