nbarbettini / little-aspnetcore-book

The Little ASP.NET Core Book, a friendly introduction to web programming and ASP.NET Core 2.0
http://littleasp.net/book
Creative Commons Attribution 4.0 International
702 stars 190 forks source link

Compatibility with ASP.NET Core 2.1 #65

Open nbarbettini opened 6 years ago

nbarbettini commented 6 years ago

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

Backhage commented 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.

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

mattwelke commented 6 years ago

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

ZoBoRf commented 6 years ago

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

mattwelke commented 6 years ago

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

ZoBoRf commented 6 years ago

@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

deman commented 6 years ago

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>>();
nbarbettini commented 6 years ago

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.

hegdevnayak commented 5 years ago

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

ghost commented 5 years ago

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?

bingoabs commented 5 years ago

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.

mattwelke commented 5 years ago

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

mihai80 commented 5 years ago

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.

shuhratjan commented 4 years ago

@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