The simplest example demonstrates user authentication and authorization in asp. net5 (3), the simplest asp. net5
Upper:
The simplest example demonstrates user authentication and authorization in asp. net5 (2)
After various management interfaces of roles are implemented, the next step is to manage users. There are many interfaces for user management, which must be implemented as follows:
1 public interface IUserRoleStore<TUser> : IUserStore<TUser>, IDisposable where TUser : class2 3 public interface IUserPasswordStore<TUser> : IUserStore<TUser>, IDisposable where TUser : class4 5 public interface IQueryableUserStore<TUser> : IUserStore<TUser>, IDisposable where TUser : class
The first is about the user-role relationship interface, which defines several role-related methods.
The second is about the password, which defines methods such as reading and setting the user password.
The last one is about users. It defines methods such as adding, deleting, modifying, and querying users.
The specific code is as follows:
1 public class HDUserStore <TUser>: 2 IUserRoleStore <TUser>, 3 IUserPasswordStore <TUser>, 4 IQueryableUserStore <TUser> 5 where TUser: HDUser, new () 6 {7 /// <summary> 8 // store user list 9 /// </summary> 10 private readonly Dictionary <string, TUser> _ users = new Dictionary <string, TUser> (); 11 12 public IQueryable <TUser> Users 13 {14 get 15 {16 if (_ users. count = 0) 17 {18 // generate user 19 TUser user = Re AteNewUser ("1", "daxia", "qingdao"); 20 // Set user Role 21 user. roles. add (new HDUserRole () {UserId = "1", RoleId = "power"}); 22 _ users. add (user. id, user); 23 24 user = CreateNewUser ("2", "aspnet5", "mvc6"); 25 user. roles. add (new HDUserRole () {UserId = "2", RoleId = "admin"}); 26 _ users. add (user. id, user); 27} 28 return _ users. values. asQueryable (); 29} 30} 31 32 // <summary> 33 // generate user 34 /// </ Summary> 35 // <param name = "id"> </param> 36 // <param name = "name"> </param> 37 // <param name = "pwd"> </param> 38 // <returns> </returns> 39 private TUser CreateNewUser (string id, string name, string pwd) 40 {41 TUser user = new TUser (); 42 user. passWord = pwd; 43 user. id = id; 44 user. userName = name; 45 user. normalizedUserName = name. toUpper (); 46 PasswordHasher <TUser> hash = new PasswordHasher <TUser> (); 47 user. passwordHash = hash. hashPassword (user, user. passWord); 48 49 return user; 50} 51 52 public Task AddToRoleAsync (TUser user, string roleId, CancellationToken cancellationToken) 53 {54 user. roles. add (new HDUserRole {RoleId = roleId, UserId = user. id}); 55 return Task. fromResult (0); 56} 57 58 public Task <IdentityResult> CreateAsync (TUser user, CancellationToken cancellationToke N) 59 {60 _ users [user. id] = user; 61 return Task. fromResult (IdentityResult. success); 62} 63 64 public Task <IdentityResult> DeleteAsync (TUser user, CancellationToken cancellationToken) 65 {66 if (user = null |! _ Users. containsKey (user. id) 67 {68 throw new InvalidOperationException ("Unknown user"); 69} 70 _ users. remove (user. id); 71 return Task. fromResult (IdentityResult. success); 72} 73 74 public void Dispose () 75 {76 77} 78 79 public Task <TUser> FindByIdAsync (string userId, CancellationToken cancellationToken) 80 {81 if (_ users. containsKey (userId) 82 {83 return Task. fromResult (_ users [userId]); 84} 85 return Task. fromResult <TUser> (null); 86} 87 88 public Task <TUser> FindByNameAsync (string userName, CancellationToken cancellationToken) 89 {90 return 91 Task. fromResult (92 Users. firstOrDefault (u => u. userName. toUpper () = userName. toUpper (); 93} 94 95 public Task <string> GetNormalizedUserNameAsync (TUser user, CancellationToken cancellationToken) 96 {97 return Task. fromResult (user. norma LizedUserName); 98} 99 100 public Task <string> GetPasswordHashAsync (TUser user, CancellationToken cancellationToken) 101 {102 return Task. fromResult (user. passwordHash); 103} 104 105 public Task <IList <string> GetRolesAsync (TUser user, CancellationToken cancellationToken) 106 {107 return Task. fromResult <IList <string> (user. roles. select (ur => ur. roleId ). toList (); 108} 109 110 public Task <string> GetUs ErIdAsync (TUser user, CancellationToken cancellationToken) 111 {112 return Task. fromResult (user. id); 113} 114 115 public Task <string> GetUserNameAsync (TUser user, CancellationToken cancellationToken) 116 {117 return Task. fromResult (user. userName); 118} 119 120 public Task <IList <TUser> GetUsersInRoleAsync (string roleName, CancellationToken cancellationToken) 121 {122 if (String. isNullOrEmpty (roleName )) 123 {124 throw new ArgumentNullException (nameof (roleName); 125} 126 127 return Task. fromResult <IList <TUser> (Users. where (u => (u. roles. where (x => x. roleId = roleName ). count ()> 0 )). select (x => x ). toList (); 128 129} 130 131 public Task <bool> HasPasswordAsync (TUser user, CancellationToken cancellationToken) 132 {133 return Task. fromResult (user. passWord! = Null); 134} 135 136 public Task <bool> IsInRoleAsync (TUser user, string roleName, CancellationToken cancellationToken) 137 {138 return Task. fromResult (user. roles. any (ur => ur. roleId = roleName); 139} 140 141 public Task RemoveFromRoleAsync (TUser user, string roleName, CancellationToken cancellationToken) 142 {143 var roleEntity = user. roles. singleOrDefault (ur => ur. roleId = roleName); 144 if (roleEn Tity! = Null) 145 {146 user. roles. remove (roleEntity); 147} 148 return Task. fromResult (0); 149} 150 151 public Task SetNormalizedUserNameAsync (TUser user, string normalizedName, CancellationToken cancellationToken) 152 {153 user. normalizedUserName = normalizedName; 154 return Task. fromResult (0); 155} 156 157 public Task SetPasswordHashAsync (TUser user, string passwordHash, CancellationToken cancellationToken) 158 {159 user. passwordHash = passwordHash; 160 return Task. fromResult (0); 161} 162 163 public Task SetUserNameAsync (TUser user, string userName, CancellationToken cancellationToken) 164 {165 user. userName = userName; 166 return Task. fromResult (0); 167} 168 169 public Task <IdentityResult> UpdateAsync (TUser user, CancellationToken cancellationToken) 170 {171 _ users [user. id] = user; 172 return Task. fromResult (IdentityResult. success); 173} 174}
The code itself is easy to understand. It should be noted that the role is also attached to the user while we generate the user. Of course, this can also be accessed through a custom database.
To be continued