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

Difficulties in getting the example code in the book running in the Authorization part.

Open ghost opened this issue 6 years ago • 7 comments

I have followed the book and it's very good. I have some difficulties in getting the code to work around the Authorization topic.

  1. ApplicationUser: I understand that I have to create this class by deriving it from IdentityUser (although I don't see it described anywhere in the book). Even when I created that class, it doesn't work.
  2. About the admin role, the example code might not be up to date to the latest Core API???

So for 1. I simply use IdentityUser and it works. for 2. I have replaced the services.AddIdentity.... in Startup.cs with this: services.AddDefaultIdentity<IdentityUser>() .AddRoles<IdentityRole>() .AddDefaultUI(UIFramework.Bootstrap4) .AddEntityFrameworkStores<ApplicationDbContext>(); It works fine for me. Hope this helps.

ghost avatar Feb 23 '19 18:02 ghost

Another thing...

in _Layout.cshtml @await Html.PartialAsync("_LoginPartial") @await Html.PartialAsync("_AdminActionsPartial")

This also doesn't work.

I have to do this instead:

                <partial name="_LoginPartial" />
                <partial name="_AdminActionsPartial" />

ghost avatar Feb 23 '19 18:02 ghost

I am having difficulties with the authorization as well. To confirm / iterate on what @psaeui wrote, I also used <IdentityUser> in place of <ApplicationUser> to get things working.

Further, in Startup.cs I used the following:

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

After these changes the DB was seeded. My test admin account shows up properly in the view that supposedly filters to user role.

However, I'm still having troubles getting the authorization for the ManageUsersController to work. When the line [Authorize(Roles = Constants.AdministratorRole)] is used above the class, my test administrator account is not able to access the page, even though the same constant is used to filter the user accounts in the DB and put them in the table as expected.

Here's what I see when I remove the [Authorize] line:

image

What am I missing here?

Here's my repo: https://github.com/raquelmsmith/dotnet-todo/tree/master/AspNetCoreTodo

Any help would be appreciated!

raquelmsmith avatar Apr 24 '19 04:04 raquelmsmith

@raquelmsmith There is a known issue in dotnet 2.1 where AddDefaultIdentity<TUser>() does not enable roles by default. This has been fixed in 2.2. I just updated my project and everything works. You do need to either create your own ApplicationUser that inherits from IdentityUser or use IdentityUser directly.

blueknightone avatar May 16 '19 17:05 blueknightone

Hi all, I have the same problem that @raquelmsmith, I even have created the ApplicationUser class, and it seem that works all except when you wanna entre in the ManagerUsers view as an admin. it's weird because Works the logic that show the link to the ManagerUsers pages if you are logged as an admin, but when you clicked on it, show the "Access denied" page... @blueknightone, I am updated my Project to .NET Core 2.2, with VS 2019, but still fail This is my repo: https://github.com/erniker/LearningASPNETCoreAndTests

It will be nice if someone could help me!

erniker avatar Jun 16 '19 15:06 erniker

While we're piling on, I'd like to add that I've been trying to figure out how to complete the authorization section for about 3 hours now. I'm new to Identity and this section doesn't work out of the box, unlike everything prior. This issue thread is the only thing that's been able to unblock me.

StevenDunn avatar Aug 14 '19 19:08 StevenDunn

Hi all, I have fixed this problem:

Keep using "ApplicationUser" image

Step 1: Go to "Startup.cs", change "services.AddDefaultIdentity()......" to "services.AddIdentity<ApplicationUser, IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders();"

Step 2: Go to web page file "AspNetCoreTodo\Views\Shared_LoginPartial.cshtml" , there are 2 old "IdentityUser" in this file, change them to "ApplicationUser" like this: image

Step 3: Go to file "AspNetCoreTodo\Data\ApplicationDbContext", change "IdentityDbContext" to "IdentityDbContext<ApplicationUser>" like this: image

Finally, run it again, then the DI will works.

Hopefully this helps u guys!

RayWangQvQ avatar Sep 05 '19 02:09 RayWangQvQ

@RayWangQvQ helped a lot. The Logout does not work though: it just seems to refresh the page.

nicholaide avatar Jul 15 '20 16:07 nicholaide