ASP.NET Core和Angular 7上的电子邮件确认(使用IdentityUser)

问题描述 投票:0回答:1

我想对我的注册过程实施电子邮件确认,我使用Angular 7作为客户端,我尝试通过教程自己实现它,但其中大多数用于MVC ...我想知道我到底需要什么,以及它应该如何工作...

这是我的代码:

ASP核心:

        [HttpPost]
        [Route("Register")]
        public async Task<object> PostAppUser(AppUserModel model)
        {
            var result = await _userService.Register(model);

            if (result != null)
                return Ok(result);
            else
                return BadRequest(new { message = "Register failed! Please try again later" });
        }

  public async Task<object> Register(AppUserModel model)
        {
            if (model.SpecialCode == _appSettings.Special_Code)
                model.Role = "admin";
            else
                model.Role = "customer";

            var appUser = new AppUser()
            {
                UserName = model.UserName,
                Email = model.Email,
                FullName = model.FullName,
            };

            try
            {
                var result = await _userManager.CreateAsync(appUser, model.Password);
                await _userManager.AddToRoleAsync(appUser, model.Role);

                return result;
            }

            catch (Exception ex)
            {
                throw ex;
            }
        }

启动:

    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    services.AddScoped<IUserService, UserService>();
    services.AddDbContext<AuthContext>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("IdentityConnection")));
    services.AddDefaultIdentity<AppUser>()
    .AddRoles<IdentityRole>()
    .AddEntityFrameworkStores<AuthContext>();

角度:

 onSubmit(form: NgForm, userName: string) {
    this.userService.login(form.value).subscribe(
      (res: any) => {
        localStorage.setItem('token', res.token);
        this.router.navigateByUrl('/home');
        this.toastr.success('Welcome ' + userName + '!' , 'Authentication granted');
      },
      (err: any) => {
        if (err.status === 400) {
          this.toastr.error('Incorrect User name of Password!', 'Authentication failed');
          form.reset();
        } else {
          this.toastr.error('Our servers is down at the moment', 'Try again later');
          form.reset();
        }
      }
asp.net-core asp.net-identity angular7
1个回答
0
投票

如果要在应用程序中实现电子邮件确认,则应:

© www.soinside.com 2019 - 2024. All rights reserved.