Giter Site home page Giter Site logo

Comments (13)

Backhage avatar Backhage commented on June 3, 2024 17

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

from little-aspnetcore-book.

ZoBoRf avatar ZoBoRf commented on June 3, 2024 9

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

from little-aspnetcore-book.

deman avatar deman commented on June 3, 2024 4

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>>();

from little-aspnetcore-book.

mattwelke avatar mattwelke commented on June 3, 2024 2

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

from little-aspnetcore-book.

nbarbettini avatar nbarbettini commented on June 3, 2024 2

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.

from little-aspnetcore-book.

bingoabs avatar bingoabs commented on June 3, 2024 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>>();

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

from little-aspnetcore-book.

mattwelke avatar mattwelke commented on June 3, 2024

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

from little-aspnetcore-book.

ZoBoRf avatar ZoBoRf commented on June 3, 2024

@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

from little-aspnetcore-book.

hegdevnayak avatar hegdevnayak commented on June 3, 2024

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

from little-aspnetcore-book.

 avatar commented on June 3, 2024

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?

from little-aspnetcore-book.

mattwelke avatar mattwelke commented on June 3, 2024

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

from little-aspnetcore-book.

mihai80 avatar mihai80 commented on June 3, 2024

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.

from little-aspnetcore-book.

shuhratjan avatar shuhratjan commented on June 3, 2024

@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

from little-aspnetcore-book.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.