Пример #1
0
        public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            var vkClaimCookies = await AuthenticationManager.AuthenticateAsync(DefaultAuthenticationTypes.ExternalCookie);

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

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    return(View("ExternalLoginFailure"));
                }
                var user = new ApplicationUser()
                {
                    UserName = model.UserName
                };
                var result = await UserManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    var vkClaim = vkClaimCookies.Identity.FindFirst("VKAccessToken");
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);

                    if (result.Succeeded)
                    {
                        var vkAuthService = new VkAuthService();
                        vkAuthService.CreateOrUpdateUserAccessToken(vkClaim.Value, user.Id);

                        await SignInAsync(user, isPersistent : false);

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

            ViewBag.ReturnUrl = returnUrl;
            return(View(model));
        }
Пример #2
0
        public async Task <ActionResult> ExternalLoginCallback(string returnUrl)
        {
            var req    = Request;
            var result = await AuthenticationManager.AuthenticateAsync(DefaultAuthenticationTypes.ExternalCookie);

            if (result == null || result.Identity == null)
            {
                return(RedirectToAction("Login"));
            }

            var idClaim = result.Identity.FindFirst(ClaimTypes.NameIdentifier);

            if (idClaim == null)
            {
                return(RedirectToAction("Login"));
            }

            var login = new UserLoginInfo(idClaim.Issuer, idClaim.Value);
            var name  = result.Identity.Name == null ? "" : result.Identity.Name.Replace(" ", "");

            // Sign in the user with this external login provider if the user already has a login
            var user = await UserManager.FindAsync(login);

            if (user != null)
            {
                var vkClaim = result.Identity.FindFirst("VKAccessToken");

                var vkAuthService = new VkAuthService();
                vkAuthService.CreateOrUpdateUserAccessToken(vkClaim.Value, user.Id);

                await SignInAsync(user, isPersistent : false);

                return(RedirectToLocal(returnUrl));
            }
            else
            {
                // If the user does not have an account, then prompt the user to create an account
                ViewBag.ReturnUrl     = returnUrl;
                ViewBag.LoginProvider = login.LoginProvider;
                return(View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel {
                    UserName = name
                }));
            }
        }