ASP.NET5 user authentication and Authorization (2) _ Practical skills

Source: Internet
Author: User

The previous article introduced the ASP.NET5 user authentication and the authorization (1), the foundation is set up, followed by the creation of classes to operate the underlying classes, that is, the implementation of the basic class of additions and deletions of course, in order to use the ASP.NET5 authentication mechanism, these are through a specific interface to achieve.

For example, for a persona, 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> creat
     Easync (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 nothing complicated, one is to get a list of all the predefined roles, the other is about the role of deletion and change, the code is as follows:

 public class hdrolestore<trole>: iqueryablerolestore<trole> where Trole:hdrole, new () {/ <summary>///stores 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 = "prawn"; _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 (RO Le = = 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 CA Ncellationtoken) {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 Ta Sk. 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.N
       ame = 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 line 12th, our method directly kills the list of roles, and if combined with specific items, I believe Ye Liangchen has 100 ways to get a list of roles from a variety of databases, profiles, etc., while the rest of the program code is essentially unchanged.

Of course, ASP.NET5 's own default implementation implements many other interfaces, and here for the simplest, it only implements the basics.

The above is to introduce you to the ASP.NET5 user authentication and authorization (2), I hope you like. Follow-up will continue to update, please continue to pay attention 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.