The simplest example demonstrates user authentication and authorization in asp. net5 (1), the simplest asp. net5
In asp. net5, user authentication and authorization provide a wealth of functions. If ef7 is used together, related database tables can be automatically generated and called conveniently.
However, it is really a headache to understand a lot of classes about authentication or to customize authentication according to the specific requirements of your project. To solve this problem, we need to fundamentally understand the authentication and authorization mechanisms. However, this is not a simple task. Some concepts are also abstract. To facilitate understanding, here I use the simplest example to demonstrate how to authenticate and authorize, and simply demonstrate the authentication and authorization itself without using ef or database.
To authenticate, you must first have a user. Here we create a user class as follows:
1 /// <summary> 2 /// user 3 /// </summary> 4 public class HDUser 5 {6 7 /// <summary> 8 // user ID 9 /// </summary> 10 public string Id {get; set;} 11 12 // <summary> 13 // login name 14 // </summary> 15 public string UserName {get; set ;} 16 17 /// <summary> 18 // standard user name 19 /// </summary> 20 public string NormalizedUserName {get; set ;} 21 22 /// <summary> 23 // PassWord 24 /// </summary> 25 public string PassWord {get; set ;} 26 27 /// <summary> 28 // The password after hash code 29 /// </summary> 30 public string PasswordHash {get; set ;} 31 32 // <summary> 33 // user's role 34 /// </summary> 35 public virtual ICollection <HDUserRole> Roles {get; private set ;} = new List <HDUserRole> (); 36 37}
Here, most of the fields in the HDUser class are easy to understand, and the NormalizedUserName is hard to understand. It can be simply considered as a UserName in upper case.
Then there is the role class:
1 /// <summary> 2 /// role 3 /// </summary> 4 public class HDRole 5 {6 /// <summary> 7 // role ID 8 /// </summary> 9 public string Id {get; set;} 10 11 /// <summary> 12 // role Name 13 /// </summary> 14 public string Name {get; set;} 15 16}
With the user and role, to establish the relationship between the user and the role, you need the User Role class:
1 /// <summary> 2 /// User Role ing 3 /// </summary> 4 public class HDUserRole 5 {6 /// <summary> 7 /// user ID 8 /// </summary> 9 public virtual string UserId {get; set;} 10 11 /// <summary> 12 // role ID13 /// </summary> 14 public virtual string RoleId {get; set;} 15}
In this way, we have all three basic classes.
Next article:
The simplest example demonstrates user authentication and authorization in asp. net5 (2)