예제 #1
0
        private async Task SignInAsync(IdentityUser user, bool isPersistent)
        {
            AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
            var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);

            AuthenticationManager.SignIn(new AuthenticationProperties()
            {
                IsPersistent = isPersistent
            }, identity);

            //var mochaUserId = new Guid(user.Id);
            //var exists = await _UserHandler.ExistsUserByMochaUserIdAsync(mochaUserId);
            //if (!exists)
            //{
            //    /// initialize user
            //    await _UserHandler.CreateUserByMochaUserIdAsync(mochaUserId);
            //    //var createdUser = await _UserHandler.CreateUserByMochaUserIdAsync(mochaUserId);
            //    //await _refListHandler.CreateUnfiledRefListAsync(createdUser.Id);
            //}
        }
예제 #2
0
        public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Manage"));
            }

            if (await _UserHandler.ExistsUserByUserNameAsync(model.UserName))
            {
                ModelState.AddModelError("", "登録済みのユーザー名です。");
                ViewBag.ReturnUrl = returnUrl;
                return(View(model));
            }
            //if (await _UserHandler.ExistsMochaUserByEmailAsync(model.Email))
            //{
            //    ModelState.AddModelError("", "登録済みのメールアドレスです。");
            //    ViewBag.ReturnUrl = returnUrl;
            //    return View(model);
            //}

            if (ModelState.IsValid)
            {
                // 外部ログイン プロバイダーからユーザーに関する情報を取得します
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();

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

                var exIdentity = await AuthenticationManager.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);

                var email = exIdentity.FindFirstValue(ClaimTypes.Email); /// twitterの場合は取れない

                var dispName = string.IsNullOrWhiteSpace(model.DisplayName) ? model.UserName : model.DisplayName;
                var user     = new User()
                {
                    UserName    = model.UserName,
                    Email       = email,
                    DisplayName = dispName,
                };
                var idUser = new IdentityUser(user);
                var result = await UserManager.CreateAsync(idUser);

                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(idUser.Id, info.Login);

                    if (result.Succeeded)
                    {
                        await SignInAsync(idUser, isPersistent : false);

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

            ViewBag.ReturnUrl = returnUrl;
            return(View(model));
        }