User Authentication and Authorization in asp. net5 of 2

  • 2021-07-02 23:53:49
  • OfStack

The last article introduced user authentication and authorization (1) in asp. net5. After the foundation is established, it is necessary to create a class that operates on the basic class, that is, to realize the addition, deletion and modification of the basic class. Of course, in order to use the authentication mechanism of asp. net5, these are all realized through specific interfaces.

For example, for roles, the interface to be implemented by role management 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, it's not complicated. One is to get a list of all predefined roles, and the other is about the addition, deletion and modification of 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 = " Users ";
           _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<IdentityResult> 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);
     }
   }

It can be seen that in line 12, the role list is written directly in our method. If combined with specific projects, I believe Ye Liangchen has 100 ways to obtain the role list from various databases, configuration files, etc., while other program codes basically need not be changed.

Of course, the default implementation that comes with asp. net5 implements many other interfaces, and only the most basic ones are implemented here for simplicity.

The above is the user authentication and authorization (2) in asp. net5 introduced to you. I hope you like it. Follow-up will continue to update, please continue to pay attention to this site.


Related articles: