Identity用戶管理入門三(注冊用戶)


用戶注冊主要有2個方法,1、密碼加密 2、用戶注冊 3、ASP.NET Core Identity 使用密碼策略、鎖定和 cookie 配置等設置的默認值。 可以在類中重寫這些設置 Startup(官方詳情點這里

首先創建CreateUserViewModel視圖模型

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace Shop.ViewModel
{
    public class CreateUserViewModel
    {
        [Required(ErrorMessage = "用戶名不能為空")]
        [DisplayName("用戶名")]
        public string UserName { get; set; }

        [EmailAddress(ErrorMessage = "郵箱格式不正確")]
        [DisplayName("郵箱")]
        public string Email { get; set; }

        [DisplayName("手機號")]
        [MinLength(11,ErrorMessage = "手機位數不足11位"), StringLength(11)]
        public string PhoneNumber { get; set; }

        [Required(ErrorMessage = "密碼不能為空")]
        [DataType(DataType.Password)]
        [DisplayName("密碼")]
        public string PasswordHash { get; set; }
    }
}

創建regiseter方法

public IActionResult Register()
{
    return View();
}

創建Register視圖

@model Shop.ViewModel.CreateUserViewModel
@{
    ViewData["Title"] = "Register";
}

<h1>Register</h1>
<form class="form-horizontal" asp-action="Register" method="post">
    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
    <fieldset>
        <div class="control-group">
            <label class="control-label">用戶名</label>
            <div class="controls">
                <input type="text" placeholder="" class="input-xlarge" asp-for="UserName">
                <span asp-validation-for="UserName" class="text-danger"></span>
            </div>
        </div>

        <div class="control-group">
            <label class="control-label">郵箱</label>
            <div class="controls">
                <input type="text" placeholder="" class="input-xlarge" asp-for="Email">
                <span asp-validation-for="Email" class="text-danger"></span>
            </div>
        </div>

        <div class="control-group">
            <label class="control-label">手機號</label>
            <div class="controls">
                <input type="text" placeholder="" class="input-xlarge" asp-for="PhoneNumber">
                <span asp-validation-for="PhoneNumber" class="text-danger"></span>
            </div>
        </div>

        <div class="control-group">
            <label class="control-label">密碼</label>
            <div class="controls">
                <input type="text" placeholder="" class="input-xlarge" asp-for="PasswordHash">
                <span asp-validation-for="PasswordHash" class="text-danger"></span>
            </div>
        </div>
        <input type="submit" class="btn btn-primary" value="注冊">
    </fieldset>
</form> @*輸入內容跟模型定義規則不符時需要驗證提示加入此腳本*@
@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

創建Register post方法

[HttpPost]
public async Task<IActionResult> Register(CreateUserViewModel input)
{
    if (ModelState.IsValid)
    {
        var user = new IdentityUser
        {
            UserName = input.UserName,
            Email = input.Email,
            PhoneNumber = input.PhoneNumber,
            PasswordHash = input.PasswordHash
        };        //創建用戶
        var result = await _userManager.CreateAsync(user);
        //如果成功則返回用戶列表
        if (result.Succeeded)
        {
            return RedirectToAction("Index");
        }
    }
    return View(input);
}

效果展示,如果驗證錯誤則有如下提示

如果成功提交則返回用戶列表頁,注意:未做編號自增及用戶名是否重復的驗證


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM