コード例 #1
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                //Generate a new confirmation token.  Here we are just storing a Guid as a string, but feel free to use whatever you want
                // (if you use another type, make sure to update the user object
                //and the user table accordingly).
                var confirmationToken = Guid.NewGuid().ToString();

                //Create the User object.  If you have customized this beyond this example, make sure you update this to contain your new fields.
                //The confirmation token in our example is ultimately for show.  Make sure to modify the RegisterViewModel and the Register view if you have customized the object.
                var user = new User {
                    UserName          = model.Email.TrimEnd(), Nickname = model.Nickname.TrimEnd(), IsConfirmed = true,
                    ConfirmationToken = confirmationToken, CreatedDate = DateTime.UtcNow
                };

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

                //If it's successful we log the user in and redirect to the home page
                if (result.Succeeded)
                {
                    //send e-mail confirmation here and instead of logging the user in and taking them to the home page, redirect them to some page indicating a confirmation email has been sent to them
                    await SignInAsync(user, false);

                    return(RedirectToAction("Index", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
コード例 #2
0
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                //Generate a new confirmation token.  Here we are just storing a Guid as a string, but feel free to use whatever you want (if you use another type, make sure to update the user object
                //and the user table accordingly).
                var confirmationToken = Guid.NewGuid().ToString();

                //Create the User object.  If you have customized this beyond this example, make sure you update this to contain your new fields.  
                //The confirmation token in our example is ultimately for show.  Make sure to modify the RegisterViewModel and the Register view if you have customized the object.
                var user = new User { UserName = model.Email.TrimEnd(), Nickname = model.Nickname.TrimEnd(), IsConfirmed = true, ConfirmationToken = confirmationToken, CreatedDate = DateTime.UtcNow };

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

                //If it's successful we log the user in and redirect to the home page
                if (result.Succeeded)
                {
                    //send e-mail confirmation here and instead of logging the user in and taking them to the home page, redirect them to some page indicating a confirmation email has been sent to them
                    await SignInAsync(user, false);
                    return RedirectToAction("Index", "Home");
                }
                AddErrors(result);
            }

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