.Net Core identity framework uses

When using the identification framework to operate users and roles, you need to complete the construction of the framework first, and refer to the article for operation:

.Net Core logo framework construction

Case 1: Create users and roles

Determine whether the role exists, create it if it does not exist; determine whether the user exists, create it if it does not exist;

using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;

namespace 鉴权与授权.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DemoController : Controller
    {
        private readonly ILogger<DemoController> logger;
        private readonly UserManager<User> userManager;
        private readonly RoleManager<Role> roleManager;

        public DemoController(ILogger<DemoController> logger, UserManager<User> userManager, RoleManager<Role> roleManager)
        {
            this.logger = logger;
            this.userManager = userManager;
            this.roleManager = roleManager;
        }

        [HttpPost]
        public async Task<ActionResult<string>> TestAction()
        {
            //判断角色是否存在
            bool roleExists = await roleManager.RoleExistsAsync("admin");
            if (!roleExists)
            {
                Role role = new Role { Name = "admin" };
                var r = await roleManager.CreateAsync(role);

                if (!r.Succeeded)
                {
                    return BadRequest(r.Errors);
                }
            }

            //判断用户是否存在
            User user = await userManager.FindByNameAsync("zhangsan");
            if (user == null)
            {
                user = new User { UserName = "zhangsan", NiceName = "张三", Email = "[email protected]", EmailConfirmed = true };
                var u = await userManager.CreateAsync(user, "123456");
                if (!u.Succeeded)
                {
                    return BadRequest(u.Errors);
                }

                var ur = await userManager.AddToRoleAsync(user, "admin");
                if (!ur.Succeeded)
                {
                    return BadRequest(ur.Errors);
                }
            }

            return "ok";

        }

    }
}

Program execution result:

 

 

Case 2: Handling login requests

LoginRequest.cs

public record LoginRequest(string UserName,string Password);

method in the controller:

        [HttpPost]
        public async Task<ActionResult<string>> LoginAction(LoginRequest request)
        {
            string userName = request.UserName;
            string password = request.Password;
            var user = await userManager.FindByNameAsync(userName);
            if (user == null)
            {
                return NotFound("用户名或密码错误");
            }

            bool isLock = await userManager.IsLockedOutAsync(user);
            if (isLock)
            {
                return NotFound("用户名已被锁定");
            }

            var isSuccess = await userManager.CheckPasswordAsync(user, password);
            if (isSuccess)
            {
                //重置错误次数
                await userManager.ResetAccessFailedCountAsync(user);
                return "用户名密码校验通过";
            }
            else
            {
                //错误次数+1
                await userManager.AccessFailedAsync(user);
            }

            return NotFound("用户名或密码错误");
        }

Case 3: Implement password reset

 method in the controller:

        /// <summary>
        /// 重置密码前发送验证码
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> SendResetPasswordToken(string userName)
        {
            var user = await userManager.FindByNameAsync(userName);
            if (user == null)
            {
                return BadRequest("用户不存在");
            }

            string token = await userManager.GeneratePasswordResetTokenAsync(user);
            logger.LogInformation($"向邮箱{user.Email},发送验证码{token}");

            return Ok();
        }
      

Results of the:

 

According to the user name and verification code, reset the password

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="newPassword">密码</param>
        /// <param name="token">验证码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> UpdatePassword(string userName, string newPassword, string token)
        {
            var user = await userManager.FindByNameAsync(userName);
            if (user == null)
            {
                return BadRequest("用户不存在");
            }

            var result = await userManager.ResetPasswordAsync(user, token, newPassword);
            if (result.Succeeded)
            {
                //重置错误次数
                await userManager.ResetAccessFailedCountAsync(user);
                return Ok("密码重置成功");
            }
            else
            {
                return BadRequest("密码重置失败");
            }
        }

 Results of the:

Guess you like

Origin blog.csdn.net/liangmengbk/article/details/130546531