Exemplo n.º 1
0
        public async Task<NotificationResult> SendResetPasswordLinkAsync(ApplicationUser user, string url)
        {
            logger.Trace("Entering SendResetPasswordLinkAsync");
            logger.Trace("User = {0}", user.Email);
            logger.Trace("Link = {0}", url);

            logger.Error("NOTIFICATION SERVICE NOT IMPLEMENTED YET = SEND FAILED");
            var result = new NotificationResult
            {
                Successful = false,
                Error = "Not Implemented"
            };
            return await Task.FromResult(result);
        }
        public async Task<IActionResult> Index(RegisterAccountVM model)
        {
            logger.Trace("POST Index()");

            if (!ModelState.IsValid) {
                logger.Info("Model is not valid - displaying form");
                return View(model);
            }

            if (!Utils.IsValidEmail(model.Email)) {
                logger.Info("Email Address {0} is not valid", model.Email);
                ModelState.AddModelError("Email", "Invalid email address");
                return View(model);
            }

            logger.Trace("Finding user {0} in database", model.Email);
            var user = await userManager.FindByNameAsync(model.Email);
            if (user != null) {
                logger.Trace("User {0} already exists - checking activation", model.Email);
                if (!user.EmailConfirmed) {
                    logger.Trace("User {0} is not activated - candidate for deletion", model.Email);
                    if (user.UserName.Equals(WebConfiguration.Instance.AdminUser)) {
                        logger.Trace("You can't fix {0} this way though!", user.UserName);
                    } else {
                        logger.Trace("Attempting to delete user {0}", model.Email);
                        var deleted = await userManager.DeleteAsync(user);
                        if (!deleted.Succeeded) {
                            logger.Error("Could not delete {0} record", model.Email);
                        }
                    }
                }
            }

            logger.Trace("Creating new user object");
            user = new ApplicationUser { UserName = model.Email, Email = model.Email };
            var result = await userManager.CreateAsync(user, model.Password);
            if (result.Succeeded) {
                logger.Trace("User creation successful - creating new Confirmation Token");
                var code = await userManager.GenerateEmailConfirmationTokenAsync(user);
                logger.Trace("Code = {0}", code);
                var callbackUrl = Url.Action("Callback", "RegisterAccount",
                    new { userId = user.Id, code = code, area = "Account" },
                    protocol: Context.Request.Scheme);
                logger.Trace("callBack URL = {0}", callbackUrl);
                if (!WebConfiguration.Instance.DevelopmentMode) {
                    logger.Trace("In Prod Mode - initiating email confirmation");
                    var notified = await Notifier.Instance.SendConfirmationLinkAsync(user, callbackUrl);
                    if (!notified.Successful) {
                        logger.Error("Notification to {0} failed", model.Email);
                        ModelState.AddModelError("", "Could not send confirmation");
                        return View(model);
                    }
                }
                ViewBag.CallbackUrl = callbackUrl;
                return View("ConfirmAccount");
            }
            logger.Trace("User creation failed - adding errors into ModelState");
            foreach (var error in result.Errors) {
                ModelState.AddModelError("", error.Description);
            }
            logger.Trace("displaying registration form (with errors");
            return View(model);
        }