예제 #1
0
        public async Task <IActionResult> Register([FromBody] RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName   = model.UserName,
                    Email      = model.Email,
                    Mobile     = model.Mobile,
                    FirstName  = model.FirstName,
                    MiddleName = model.MiddleName,
                    LastName   = model.LastName
                };

                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);

                    var userActivation = new UserActivation
                    {
                        Id           = Guid.NewGuid().ToString(),
                        Created      = DateTime.Now,
                        Expires      = DateTime.Now.AddMinutes(ActivationExpiryInMinutes),
                        IdentityCode = code,
                        UserId       = user.Id,
                        ShortCode    = Health.Configuration.Extensions.GetRandomNumber()
                    };

                    await _doctorManager.AddUserActivation(userActivation);

                    //Send SMS with the short code
                    _smsSender.Send(model.Mobile, "Activation code: " + userActivation.ShortCode);

                    try
                    {
                        if (!string.IsNullOrWhiteSpace(model.Email))
                        {
                            await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl);
                        }
                    }
                    catch // Ignored
                    {
                    }

                    _logger.LogInformation("User created a new account with password.");
                    return(Ok(new { Success = true }));
                }
                AddErrors(result);
            }

            return(BadRequest(new { Success = false, Msg = "Invalid model" }));
        }