User authentication and authorization in asp. net5 (2), asp. net5 authorization

Source: Internet
Author: User

User authentication and authorization in asp. net5 (2), asp. net5 authorization

The previous article introduced asp. in net5, user authentication and authorization (1). After the foundation is set up, you need to create a class to operate on the basic class, that is, to add, delete, modify, and query the basic class. Of course, in order to use asp. net5 authentication mechanisms are implemented through specific interfaces.

For example, the role management interface is as follows:

   public interface IQueryableRoleStore<TRole> : IRoleStore<TRole>, IDisposable where TRole : class   {     IQueryable<TRole> Roles { get; }   }   public interface IRoleStore<TRole> : IDisposable where TRole : class   {     Task<IdentityResult> CreateAsync(TRole role, CancellationToken cancellationToken);     Task<IdentityResult> DeleteAsync(TRole role, CancellationToken cancellationToken);     Task<TRole> FindByIdAsync(string roleId, CancellationToken cancellationToken);     Task<TRole> FindByNameAsync(string normalizedRoleName, CancellationToken cancellationToken);     Task<string> GetNormalizedRoleNameAsync(TRole role, CancellationToken cancellationToken);     Task<string> GetRoleIdAsync(TRole role, CancellationToken cancellationToken);     Task<string> GetRoleNameAsync(TRole role, CancellationToken cancellationToken);     Task SetNormalizedRoleNameAsync(TRole role, string normalizedName, CancellationToken cancellationToken);     Task SetRoleNameAsync(TRole role, string roleName, CancellationToken cancellationToken);    Task<IdentityResult> UpdateAsync(TRole role, CancellationToken cancellationToken);   }

In fact, there is no complexity. One is to obtain a list of all predefined roles, and the other is to add, delete, modify, and query roles. The Code is as follows:

Public class HDRoleStore <TRole>: IQueryableRoleStore <TRole> where TRole: HDRole, new () {// <summary> // store all predefined roles /// </summary> private readonly Dictionary <string, TRole> _ roles = new Dictionary <string, TRole> (); // <summary> // all Roles // </summary> public IQueryable <TRole> roles {get {if (_ Roles. count =) {TRole role = new TRole (); role. id = "admin"; role. name = "Administrator"; _ roles. add (role. Id, role); role = new TRole (); role. id = "user"; role. name = "user"; _ roles. add (role. id, role); role = new TRole (); role. id = "power"; role. name = "prawns"; _ roles. add (role. id, role);} return _ roles. values. asQueryable () ;}} public Task <IdentityResult> CreateAsync (TRole role, CancellationToken cancellationToken) {_ roles [role. id] = role; return Task. fromResult (IdentityResult. success);} public Task <Ide NtityResult> DeleteAsync (TRole role, CancellationToken cancellationToken) {if (role = null |! _ Roles. containsKey (role. id) {throw new InvalidOperationException ("Unknown role");} _ roles. remove (role. id); return Task. fromResult (IdentityResult. success);} public void Dispose () {} public Task <TRole> FindByIdAsync (string roleId, CancellationToken cancellationToken) {if (_ roles. containsKey (roleId) {return Task. fromResult (_ roles [roleId]);} return Task. fromResult <TRole> (null);} public Task <TRole> FindByNameAsync (string normalizedRoleName, CancellationToken cancellationToken) {return Task. fromResult (Roles. singleOrDefault (r => String. equals (r. name, normalizedRoleName, StringComparison. ordinalIgnoreCase);} public Task <string> GetNormalizedRoleNameAsync (TRole role, CancellationToken cancellationToken) {return Task. fromResult (role. name);} public Task <string> GetRoleIdAsync (TRole role, CancellationToken cancellationToken) {return Task. fromResult (role. id);} public Task <string> GetRoleNameAsync (TRole role, CancellationToken cancellationToken) {return Task. fromResult (role. name);} public Task SetNormalizedRoleNameAsync (TRole role, string normalizedName, CancellationToken cancellationToken) {role. name = normalizedName; return Task. fromResult ();} public Task SetRoleNameAsync (TRole role, string roleName, CancellationToken cancellationToken) {role. name = roleName; return Task. fromResult ();} public Task <IdentityResult> UpdateAsync (TRole role, CancellationToken cancellationToken) {_ roles [role. id] = role; return Task. fromResult (IdentityResult. success );}}

As you can see, in row 12th, the role list is directly written into our method. If we use a specific project together, I believe ye liangchen has one hundred methods to retrieve the role list from various databases and configuration files, while other program codes do not need to be changed.

Of course, the default Implementation of asp. net5 implements many other interfaces. Here, only the most basic interfaces are implemented for the sake of simplicity.

In asp. net5, user authentication and authorization (2) are described as follows. It will be updated in the future. Please stay tuned to this site.

Related Article

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.