Adding and removing roles for users with ASP. NET Core Identity

Source: Internet
Author: User

Compiling a project based on an ASP. NET core requires the user to add and remove roles, and then uses the Usermanager in the identity.

Has solved several problems, finally realized the idea.

1. Environmental conditions

ASP. NET Core 1.0.1

Microsoft.AspNetCore.Identity.EntityFrameworkCore 1.0.0

2. Add a role (group) to the user using the Usermanager.addtorolesasync (), the Addtorolesasync in the metadata is interpreted as:

        //        //Summary://ADD The specified user to the named roles. //        //Parameters://User://The user to add to the named roles. //        //roles://The name of the roles to add the user to. //        //return Result://the System.Threading.Tasks.Task that represents the asynchronous operation, containing//The Microsoft.AspNetCore.Identity.IdentityResult of the operation.[Asyncstatemachine (typeof(usermanager<>.<addtorolesasync>d__100)]  Public VirtualTask<identityresult> Addtorolesasync (TUser user, ienumerable<string> Roles);

The type that corresponds to the first parameter in my code is Appcationuser, and the second parameter should be a list of strings that represent the role (group);

The line code in the controller is:

await _usermanager.addtorolesasync (user, Selectedroles)

In the default role table, there are two fields with a role name, one is "name" and the other is "Normalizedname", as shown in:

After an attempt, the second parameter in Addtorolesasync () should be "Normalizedname". If you use "Name" there will be an error.

3. Delete the user's existing role will be used to Usermanager.removefromroleasync (), this method will also use to Addtorolesasync () two parameters, using the same method as Addtorolesasync ().

However, the identity provides the only way to get the user's existing role: Usermanager.getrolesasync (user), where the parameter "user" is <ApplicationUser>.

The return value of this method is interpreted from the metadata as: role names. The test is actually the "Name" field.

        //        //Summary://Gets A list of role names the specified user belongs to. //        //Parameters://User://The user whose role names to retrieve. //        //return Result://the System.Threading.Tasks.Task that represents the asynchronous operation, containing//a list of role names.[Asyncstatemachine (typeof(usermanager<>.<getrolesasync>d__105)]  Public Virtualtask<ilist<string>> Getrolesasync (TUser user);

In this way, there is an inconvenient problem, delete the user role method Removefromroleasync (), need to use "normalizedname", and the identity gets the current user existing role can only use Getrolesasync (), This method obtains the "Name" of the role.

So the code that is taken for granted (see below) does not have a compile error, and there is no running error, but the role that should be deleted is actually removed after the actual run:

var nowroles =//Get Nowroles is the name of the roleawait//What is needed here is the role of Normalizedname

Therefore, we can only find a way to get the "normalizedname" of the current user role, and then pass it to Removefromroleasync ().

This method is more cumbersome, but it is not easy to think of a simpler approach.

var normalizedname = Allroles.where (r = = R.name = = nowrole). First (). Normalizedname;

of which: Allroles is the way to get

var allroles = _rolemanager.roles;

The entire code is like this:

                varAllroles =_rolemanager.roles;//All roles in the systemvarNowroles =_usermanager.getrolesasync (user). Result; The user's existing role ' Name 'foreach(varNowroleinchnowroles) {                    varNormalizedname = Allroles.where (r = r.name = = nowrole). First (). Normalizedname;//get the normalizedname of an existing role                    await_usermanager.removefromroleasync (user, normalizedname);//Remove the role}await_usermanager.addtorolesasync (user, selectedroles);//Add Required rolesawait_usermanager.updateasync (user); Complete storage

The above code is not targeted to add, the code is a bit cumbersome, but this will not appear wrong, save things.

At this point, the end.

Record, for reference.

Adding and removing roles for users with ASP. NET Core Identity

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.