コード例 #1
0
        public async Task <IActionResult> addEmail(AddEmailViewModel model)
        {
            var user = await GetCurrentUserAsync();

            UserEmailAddresses email = new UserEmailAddresses();

            email.ApplicationUser   = user;
            email.ApplicationUserId = user.Id;
            email.EmailType         = model.emailTypes;
            email.emailAddress      = model.email;
            email.Confirmed         = false;
            var success = _emailProvider.createEmailForUser(email);

            if (success)
            {
                var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                //Add the email address to the end of the token. When the token is returned we will know for which email.
                code = code + "./" + model.email;
                var callbackUrl = Url.Action("ConfirmAddEmail", "Identity", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
                //This method will send an email from [email protected] to the email the user inputted.
                await _authEmail.SendEmailAsync(model.email, "Confirm Email", "Please click this <a href=\"" + callbackUrl + "\">link</a> to finish adding this email to your Identity Right Account.");

                return(RedirectToAction("ManageUserEmails", new { Message = ManageMessageId.AddEmailSuccessVerify }));
            }
            return(RedirectToAction("ManageUserEmails", new { Message = ManageMessageId.AddEmailFail }));
        }
コード例 #2
0
        public async Task <IActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                string tempIRID  = "";
                string pChars    = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
                bool   idSuccess = false;

                while (idSuccess == false)
                {
                    Random rnd = new Random();
                    tempIRID = "";

                    for (int j = 0; j < 7; j++)
                    {
                        int  rndNum = rnd.Next(0, pChars.Length - 1);
                        char c      = pChars[rndNum];
                        tempIRID = string.Format("{0}{1}", tempIRID, c);
                    }

                    //Create an instance of AppDbC
                    ApplicationDbContext adc = new ApplicationDbContext();

                    //Get a list of all the items in the Db that match the generated IRID
                    List <ApplicationUser> rowList = adc.Users.Where(item => item.IRID.Equals(tempIRID))
                                                     .ToList();

                    //If that list is empty then the IRID is free to be used.
                    if (rowList.Count == 0)
                    {
                        idSuccess = true;
                    }
                }


                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email, IRID = tempIRID, FirstName = model.FirstName, LastName = model.LastName
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    // Generate a code which is specified for a particular user.
                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    //Create a URL which will call the Account Controller ConfirmEmail method.
                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
                    //This method will send an email from [email protected] to the email the user inputted.
                    await _authEmail.SendEmailAsync(model.Email, "Confirm Email", "Please confirm your account by clicking this <a href=\"" + callbackUrl + "\">link</a>");

                    //uncomment below if we want the user to be signed in automatically. (Not recommended)
                    //await _signInManager.SignInAsync(user, isPersistent: false);
                    _logger.LogInformation(3, "User created a new account with password.");
                    return(RedirectToAction(nameof(HomeController.Index), "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }