User Authentication and Authorization in asp. net5 of 1

  • 2021-07-02 23:54:10
  • OfStack

In the last period of time, Microsoft has made great moves again. In terms of IDE, besides releasing Viausl Studio 2013 Community Edition, it also released a brand-new Visual Studio 2015 Preview.

In asp. net5, user authentication and authorization provide very rich functions. If combined with ef7, relevant database tables can be automatically generated, and it is also very convenient to call.

However, it is really a headache to understand so many classes about authentication authorization, or to customize authentication authorization according to the specific requirements of your own project. To solve this problem, Need to understand the mechanism of authentication and authorization fundamentally, but this is not a simple thing, and some concepts are abstract. In order to understand conveniently, I use the simplest example here to demonstrate how to authenticate and authorize, and it is in the case of not using ef and database, simply demonstrating authentication and authorization itself.

To authenticate, you must first have users. Here we establish a user class as follows:


/// <summary>
 ///  Users 
 /// </summary>
 public class HDUser
 {
  /// <summary>
  ///  Users ID
  /// </summary>
  public string Id { get; set; }
  /// <summary>
  ///  Login name 
  /// </summary>
  public string UserName { get; set; }
  /// <summary>
  ///  Canonical user name 
  /// </summary>
  public string NormalizedUserName { get; set; }
  /// <summary>
  ///  Password 
  /// </summary>
  public string PassWord { get; set; }
  /// <summary>
  ///  Hash encoded password 
  /// </summary>
  public string PasswordHash { get; set; }
  /// <summary>
  ///  Roles that users have 
  /// </summary>
  public virtual ICollection<HDUserRole> Roles { get; private set; } = new List<HDUserRole>();
 }

Here, most of the fields of HDUser class are easy to understand, while that NormalizedUserName is difficult to understand, which can be simply considered as the capital form of UserName.

Then there are role classes:


 /// <summary>
 ///  Role 
 /// </summary>
 public class HDRole
 {
  /// <summary>
  ///  Role ID
  /// </summary>
  public string Id { get; set; }
  /// <summary>
  ///  Role name 
  /// </summary>
  public string Name { get; set; }
 }

With users and roles, to establish the relationship between users and roles, you need user role classes:


/// <summary>
 ///  User role correspondence 
 /// </summary>
 public class HDUserRole
 {
  /// <summary>
  ///  Users ID
  /// </summary>
  public virtual string UserId { get; set; }
  /// <summary>
  /// Role ID
  /// </summary>
  public virtual string RoleId { get; set; }
 }

In this way, we have established the most basic three classes.

Ok, the above introduces the user authentication and authorization in asp. net5 (1) through a simple example, and continues to introduce the user authentication and authorization in asp. net5 (2) in the next article. Just click and view it among friends who need it.


Related articles: