Theme:
I will continue to introduce the identity of the account simple management, that is, add and delete changes. I will only introduce additions and deletions, modify the function code I will upload to my GitHub,
To create a user:
1. I create a CreateModel.cs file in the Model folder to accept user input to create account information, the code is as follows:
1 using System.ComponentModel.DataAnnotations; 2 3 Namespace Demouser.models 4 {5 Public class Createmodel 6 {7 [Required] 8 public string Name {g Et Set } 9 [required]10 public string Email {get; set;} One [required]12 public string Password {get; set;} }14}
I used the model validation feature here.
2. In the admin controller, define the method to create the account, the code is as follows:
1 public iactionresult Create () = View (); 2 3 [HttpPost] 4 public Async TASK<IACTIONRESULT&G T Create (Createmodel model) 5 {6 if (Modelstate.isvalid) 7 {8 Appuser user = new Appuser 9 {Ten UserName = model. name,11 Email = model. Email12};13 var result = await _usermanager.createasync (user, model. Password), if (result. SUCCEEDED) {+//_passwordhasher.hashpassword (); +-Return Redire Cttoaction ($ "Index"),}19 else20 {(var er Ror in result. Errors) {Modelstate.addmodelerror ("", error. Description)}25}26}27 return View (model); 29}
The code is simple, and I'll describe the role of the Identityresult class, which represents the result of creating an account, where
The Succeeded property indicates whether the operation was successful.
The Errors property contains a collection of Identityerror objects.
The Identityerror object describes the error message, Identityerror has a public string Description {get; set;} Property represents the error message description
Identityerror code is relatively simple I will not show you the anti-compilation code.
3. Create a View
Create a view corresponding to the Create method, with the following code:
1 @model Createmodel 2 <div class= "bg-primary m-1 p-1 text-white" > 3
4. Running results show:
Subsequent:I only introduced the process of creating a customer, deleting and modifying the process and creating the same, I don't introduce it, I put the code on my GitHub address Https://github.com/bluetianx/AspnetCoreExample branch is F3
Next I will introduce the principle of password authentication, and the source code introduction, and how to implement a custom password authentication class
Deep understanding of the identity of the ASPNET core (2)