AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

Problem with Roles, please help!

Open alcastronava opened this issue 8 years ago • 7 comments

I follow this article to create my own custom user data, then I follow this other to add Admin user and create and asign Admin Role

https://social.technet.microsoft.com/wiki/contents/articles/51333.asp-net-core-2-0-getting-started-with-identity-and-role-management.aspx

but I can not add roles using the UserManager and RoleManager I have this Exception: NotSupportedException: Store does not implement IUserRoleStore<TUser> When try to call "UserManager.AddToRoleAsync"

Thanks


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

alcastronava avatar Sep 24 '18 18:09 alcastronava

Fixed! Thanks anyway. I must delete the autogenerate IdentityHostingStartup.Configure method

alcastronava avatar Sep 24 '18 18:09 alcastronava

@alcastronava glad you got it fixed.

Rick-Anderson avatar Sep 24 '18 19:09 Rick-Anderson

Just want to thank alcastronava. I didn't see IdentityHostingStartup.Configure was automatically created. Pain in the ass. Thanks for figuring it out!

carlove60 avatar Feb 07 '19 22:02 carlove60

Welcome!

alcastronava avatar Feb 07 '19 23:02 alcastronava

well, it didnt fix my issue. Everyday I am questioning my decision to choose ms stack!!!!

msstop avatar Apr 30 '19 14:04 msstop

Everything is so branched out. Either you can not follow their guide since it is fixed to a set of versions that is almost impossible to provide or when you follow it, it does not work. Then you are old oh your bla bla version is bla bla!!!!!

msstop avatar Apr 30 '19 14:04 msstop

Thanks for the idea you provided, which help me a lot in using Identity in asp.net core 2.2 In asp.net core 2.2, when you need to use the userManager.addToRole(user,role) method to use the role based identity, you can follow the official guidance in here (which is the guidance actually in asp.net core 2.1): https://docs.microsoft.com/zh-cn/aspnet/core/security/authorization/secure-data?view=aspnetcore-2.2

It is actually same and the only different procedure is :

when you meet this chapter :

Add Role services to Identity

Just ignore the official docs and to do the following step :

replace the row

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

with

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

in Areas/Identity/Data/IdentityHostingStartup.cs IdentityHostingStartup::Configure method, but not add it in program.cs (the file can't be deleted in asp.net core 2.2)

I have made a duplicate declaration in AddDefaultIdentity at first ( I just follow the guidance and add it in Program.cs, which will remain a same method in IdentityHostingStartup.Configure ). And that brings me a exception : "Store does not implement IUserRoleStore"

The project will be updated later in my repos : https://github.com/ZyuHoiMing/UwpHelper , Good Luck :)

KaimingZhu avatar May 26 '19 17:05 KaimingZhu