【问题标题】:Confirm Email in asp.net core web api在 asp.net core web api 中确认电子邮件
【发布时间】:2020-08-16 11:42:24
【问题描述】:

此 API 适用于移动应用程序。目标是让用户在注册时确认电子邮件。当用户注册时,会生成一个确认链接并通过电子邮件发送。我在 MVC 项目中以同样的方式完成了它,它运行良好,但在 Web API 项目中看起来它不会削减。 现在,当用户单击该链接时,应点击相应的操作方法并完成工作。

唯一的问题是,ConfirmEmail 操作方法在单击确认链接时没有被触发,尽管它看起来不错。

以下是可能有帮助的主要配置

MVC 服务配置

services.AddMvc(options => 
                    {  
                        options.EnableEndpointRouting = true;
                        options.Filters.Add<ValidationFilter>();
                    })
                    .AddFluentValidation(mvcConfiguration => mvcConfiguration.RegisterValidatorsFromAssemblyContaining<Startup>())
                    .SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_3_0);

身份服务

 public async Task<AuthenticationResult> RegisterAsync(string email, string password)
        {
            var existingUser = await _userManager.FindByEmailAsync(email);

            if(existingUser != null)
            {
                return new AuthenticationResult { Errors = new[] { "User with this email address exists" } };
            }

            // generate user
            var newUser = new AppUser
            {
                Email = email,
                UserName = email
            };

            // register user in system
            var result = await _userManager.CreateAsync(newUser, password);

            if (!result.Succeeded)
            {
                return new AuthenticationResult
                {
                    Errors = result.Errors.Select(x => x.Description)
                };
            }

            // when registering user, assign him user role, also need to be added in the JWT!!!
            await _userManager.AddToRoleAsync(newUser, "User");

            // force user to confirm email, generate token
            var token = await _userManager.GenerateEmailConfirmationTokenAsync(newUser);

            // generate url
            var confirmationLink = _urlHelper.Action("ConfirmEmail", "IdentityController",
                    new { userId = newUser.Id, token = token }, _httpRequest.HttpContext.Request.Scheme);

            // send it per email
            var mailresult = 
                await _emailService.SendEmail(newUser.Email, "BingoApp Email Confirmation",
                $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(confirmationLink)}'>clicking here</a>.");
            if (mailresult)
                return new AuthenticationResult { Success = true };
            else
                return new AuthenticationResult { Success = false, Errors = new List<string> { "Invalid Email Address"} };
        }

控制器

        [HttpPost(ApiRoutes.Identity.Register)]
        public async Task<IActionResult> Register([FromBody] UserRegistrationRequest request)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(new AuthFailedResponse
                {
                    Errors = ModelState.Values.SelectMany(x => x.Errors.Select(xx => xx.ErrorMessage))
                });
            }

            // register the incoming user data with identity service
            var authResponse = await _identityService.RegisterAsync(request.Email, request.Password);          

            if (!authResponse.Success)
            {
                return BadRequest(new AuthFailedResponse
                {
                    Errors = authResponse.Errors
                });
            }

            // confirm registration
            return Ok();
        }


        [HttpGet]
        public async Task<IActionResult> ConfirmEmail(string userId, string token)
        {
            if (userId == null || token == null)
            {
                return null;
            }

            var user = await _userManager.FindByIdAsync(userId);

            if (user == null)
            {
                return null;
            }

            var result = await _userManager.ConfirmEmailAsync(user, token);

            if (result.Succeeded)
            {
               await _emailService.SendEmail(user.Email, "BingoApp - Successfully Registered", "Congratulations,\n You have successfully activated your account!\n " +
                    "Welcome to the dark side.");
            }

            return null;
        }

【问题讨论】:

  • 您的_urlHelper.Action(..) 看起来很可疑。我不确定您是否应该传递完整的控制器名称,即包括实际的单词控制器。请改用_urlHelper.Action("ConfirmEmail", "Identity",
  • @Dennis1679 谢谢朋友,这就是问题所在。而且我可能应该在深夜停止编码:)
  • 也许你会很友好地接受我的回答:)

标签: c# asp.net-core asp.net-identity asp.net-core-webapi


【解决方案1】:

你的_urlHelper.Action(..) 在我看来有点可疑。

我不确定您是否应该传递控制器全名,即包括实际单词controller

改用_urlHelper.Action("ConfirmEmail", "Identity",

作为提示:我尝试通过使用 nameof(IdentityController) 来避免使用此类魔术字符串,因为它会返回不带控制器后缀的控制器名称。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    • 2023-04-04
    • 2015-02-17
    • 2016-08-06
    • 2018-12-07
    • 1970-01-01
    • 2018-12-15
    相关资源
    最近更新 更多