コード例 #1
0
        public async Task <ActionResult> Register(RegisterViewModel model, HttpPostedFileBase attachment)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName    = model.Email,
                    Email       = model.Email,
                    FirstName   = model.FirstName,
                    LastName    = model.LastName,
                    DisplayName = model.DisplayName,
                    AvatarUrl   = WebConfigurationManager.AppSettings["DefaultAvatar"]
                };
                if (AttachmentUploadValidator.IsWebFriendlyImage(attachment))
                {
                    var fileName = Path.GetFileName(attachment.FileName);
                    attachment.SaveAs(Path.Combine(Server.MapPath("~/Avatars/"), fileName));
                    user.AvatarUrl = "/Avatars/" + fileName;
                }
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    //How do I recreate the sending of the email?
                    //Step 1: I need a MailMessage
                    var emailFrom = WebConfigurationManager.AppSettings["emailto"];
                    var email     = new MailMessage(emailFrom, model.Email)
                    {
                        Subject    = "Confirm your account",
                        Body       = "Please confirm your account by clicking < a href =\"" + callbackUrl + "\">here</a>",
                        IsBodyHtml = true
                    };
                    //Step 2: I need an instance of PersonalEmail
                    var svc = new PersonalEmail();
                    //Step 3: Fire method from PersonalEmail class
                    await svc.SendAsync(email);

                    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(LoginRegisterViewModel model, HttpPostedFileBase avatarPath)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName    = model.RegisterVM.DisplayName,
                    DisplayName = model.RegisterVM.DisplayName,
                    Email       = model.RegisterVM.Email,
                    FirstName   = model.RegisterVM.FirstName,
                    LastName    = model.RegisterVM.LastName,
                    AvatarPath  = model.RegisterVM.AvatarPath,
                };

                if (avatarPath != null)
                {
                    if (AttachmentUploadValidator.IsWebFriendlyImage(avatarPath))
                    {
                        var fileName     = Path.GetFileName(avatarPath.FileName);
                        var justFileName = Path.GetFileNameWithoutExtension(fileName);
                        justFileName = StringUtilities.URLFriendly(justFileName);
                        fileName     = $"{justFileName}_{DateTime.Now.Ticks}{Path.GetExtension(fileName)}";
                        avatarPath.SaveAs(Path.Combine(Server.MapPath("~/Avatars/"), fileName));
                        user.AvatarPath = "/Avatars/" + fileName;
                    }
                }

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

                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    //string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    //var callbackUrl = Url.Action("ComfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    //await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\"here</a>");

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