User authentication and authorization in asp. net5 (1), asp. net5 authorization
In recent times, Microsoft has made another major event. In addition to releasing Viausl Studio 2013 Community edition for IDE, it has also released a brand new Visual Studio 2015 Preview.
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:
/// <Summary> /// user /// </summary> public class HDUser {// <summary> // user ID // </summary> public string id {get; set ;}//< summary> /// Logon Name //</summary> public string UserName {get; set ;} /// <summary> /// standard user name /// </summary> public string NormalizedUserName {get; set ;} /// <summary> /// PassWord /// </summary> public string PassWord {get; set ;} /// <summary> /// password after hash encoding /// </summary> public string PasswordHash {get; set ;} /// <summary> /// role of the user /// </summary> public virtual ICollection <HDUserRole> Roles {get; private set ;} = new List <HDUserRole> ();}
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:
/// <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 the user and role, to establish the relationship between the user and the role, you need the User Role class:
/// <Summary> /// user-role ing // </summary> public class HDUserRole {/// <summary> // user ID // </summary> public virtual string UserId {get; set ;}//< summary> /// role ID /// </summary> public virtual string RoleId {get; set ;}}
In this way, we have all three basic classes.
Now, asp is introduced through a simple example. in net5, user authentication and authorization (1). Next I will continue to introduce asp to you. in net5, user authentication and authorization (2). You can click to view it among friends you need.