Esempio n. 1
0
        public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (!this.IsCaptchaValid(""))
            {
                ViewBag.ErrorMessage = "驗證碼錯誤";
            }

            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Index", "Manage"));
            }

            if (ModelState.IsValid)
            {
                // 從外部登入提供者處取得使用者資訊
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    return(View("ExternalLoginFailure"));
                }

                var user = new ApplicationUser
                {
                    UserName    = model.Email,
                    Email       = model.Email,
                    Name        = model.Name,
                    Sex         = model.Sex,
                    Birthday    = model.Birthday,
                    PhoneNumber = model.PhoneNumber,
                    Address     = model.Address
                };
                var result = await UserManager.CreateAsync(user);

                CopyCustomerData copyCustomer = new CopyCustomerData();
                var copyResult = copyCustomer.CopyToCustomer(model);
                if (result.Succeeded && copyResult.IsSuccessful)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);

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

                        return(RedirectToLocal(returnUrl));
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return(View(model));
        }
Esempio n. 2
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (!this.IsCaptchaValid(""))
            {
                ViewBag.ErrorMessage = "答案錯誤";
            }
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName    = model.Email,
                    Email       = model.Email,
                    Name        = model.Name,
                    Sex         = model.Sex,
                    Birthday    = model.Birthday,
                    PhoneNumber = model.PhoneNumber,
                    Address     = model.Address
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    CopyCustomerData copyCustomer = new CopyCustomerData();
                    var copyResult = copyCustomer.CopyToCustomer(model);
                    if (result.Succeeded && copyResult.IsSuccessful)
                    {
                        await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                        // 如需如何進行帳戶確認及密碼重設的詳細資訊,請前往 https://go.microsoft.com/fwlink/?LinkID=320771
                        // 傳送包含此連結的電子郵件
                        // 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, "確認您的帳戶", "請按一下此連結確認您的帳戶 <a href=\"" + callbackUrl + "\">這裏</a>");

                        return(RedirectToAction("Index", "Home"));
                    }
                }
                AddErrors(result); //0609 fix by Lei
            }

            // 如果執行到這裡,發生某項失敗,則重新顯示表單
            return(View(model));
        }