欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net5中用户认证与授权(2)

程序员文章站 2024-02-13 12:56:04
上篇文章给大家介绍了),基础建立好了,紧接着就要创建对基础类进行操作的类,也就是实现基础类的增删改查当然,为了使用asp.net5的认证机制,这些都是通过特定的接口来实现的...

上篇文章给大家介绍了),基础建立好了,紧接着就要创建对基础类进行操作的类,也就是实现基础类的增删改查当然,为了使用asp.net5的认证机制,这些都是通过特定的接口来实现的。

比如,对于角色来说,角色管理要实现的接口如下:

   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);
   }

其实,也没什么复杂,一个是获得所有预定义角色的列表,另一个是关于角色的增删改查而已,代码如下:

 public class hdrolestore<trole> : iqueryablerolestore<trole>
      where trole : hdrole, new()
    {
      /// <summary>
      /// 存储所有预定义的角色
      /// </summary>
      private readonly dictionary<string, trole> _roles = new dictionary<string, trole>();
      /// <summary>
     /// 所有角色
     /// </summary>
     public iqueryable<trole> roles
     {
       get
       {
         if (_roles.count == )
         {
           trole role = new trole();
           role.id = "admin";
           role.name = "管理员";
           _roles.add(role.id, role);
           role = new trole();
           role.id = "user";
           role.name = "用户";
           _roles.add(role.id, role);
           role = new trole();
           role.id = "power";
           role.name = "大虾";
           _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);
     }
   }

可以看到,在第12行,我们的方法里直接写死了角色列表,如果相结合具体的项目的话,我相信叶良辰有一百种方法从各种数据库、配置文件等取得角色列表,而其他程序代码却基本不用更改。

当然,asp.net5自带的默认实现实现了很多其他接口,这里为了最简单起见,只实现了最基本的。

以上就是给大家介绍的asp.net5中用户认证与授权(2),希望大家喜欢。后续还会持续更新,请大家持续关注本站。