User Registration with ASP.NET Core Identity

User Registration with ASP.NET Core Identity

So basically, we have all the input fields from our model in this view. Of course, clicking the Create button will direct us to the POST Register method with the UserRegistrationModel populated.

Now, let’s install the AutoMapper library, so we could map the UserRegistrationModel to the User class:

 

As you can see, the action is async now because the UserManager’s helper methods are async as well. Inside the action, we check for the model validity and if it is invalid, we just return the same view with the invalid model.

If that check passes, we map the registration model to the user.

Additionally, we use the CreateAsync method to register the user. But this method does more for us. It hashes a password, executes an additional user checks and returns a result.

If the result is successful, we just attach the default role to the user, again with the UserManager’s help, and redirect the user to the Index page.

But if the registration fails, we loop through all the errors and add them to the ModelState.

 

创建的用户保存到了AspNetUsers用户表,并且添加了默认的role,会在AspNetUserRoles添加用户和角色的关系。

 

Identity Options

As you could see in the testing example, our user registration process requires a password to fulfill certain rules. These rules can be found and adjusted in the IdentityOptions settings.

Right now, the password requires at least 6 characters, upper and lower case letters, etc.

Let’s play around a bit with these values.

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2022-07-13 net use映射网盘和本地磁盘 How to Map Network Drives From the Command Prompt in Windows
2021-07-13 What is difference between “git checkout -f” and “git reset --hard HEAD”?
2021-07-13 What are the differences between .gitignore and .gitkeep?
2020-07-13 Comparison Review: Microsoft SSDT vs Redgate SQL Source Control
2020-07-13 Azure DevOps Azure Repos Git How-to Guides Use SSH key authentication
2019-07-13 de4dot 反混淆
2019-07-13 GreyMagic
点击右上角即可分享
微信分享提示