예제 #1
0
        public void ExternalLoginConfirmationView_Template_Test()
        {
            var template = new ExternalLoginConfirmation(new SmartAppInfo {
                Id = ApplicationId
            });
            var output = template.TransformText();

            Assert.NotNull(output);
            Assert.NotEmpty(output);
            Assert.Contains($"@model {ApplicationId}.Backend.Models.ExternalLoginConfirmationViewModel", output);
        }
예제 #2
0
        public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmation model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Index", "Home", new { Area = "" }));
            }

            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 {
                    PosterName = TextEditor.CleanPosterName(model.PosterName), ContactName = TextEditor.CleanPosterName(model.PosterName), Email = model.Email, UserName = model.Email
                };
                var result = await UserManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    // Add to role
                    await UserManager.AddToRoleAsync(user.Id, "User");

                    // Set email confirm
                    // confirm if user is not already confirmed
                    if (!user.EmailConfirmed)
                    {
                        var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                        await UserManager.ConfirmEmailAsync(user.Id, code);
                    }
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);

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

                        user.LastLogin = DateTime.Now;
                        // transfer
                        TransferCookieDataToUser(user.Id);
                        return(RedirectToLocal(returnUrl));
                    }
                }
                ViewBag.LoginProvider = info.Login.LoginProvider;
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return(View(model));
        }
        public async Task <IActionResult> ExternalLoginConfirmation(ExternalLoginConfirmation model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var externalLoginInfo = await _signInManager.GetExternalLoginInfoAsync();

            if (externalLoginInfo == null)
            {
                return(View("Error"));
            }

            var applicationUser = new AppUser()
            {
                Email    = model.Email,
                Name     = model.Name,
                UserName = model.Email
            };

            var createdUser = await _userManager.CreateAsync(applicationUser);

            if (createdUser.Succeeded)
            {
                var addExternalLoginToCreatedUser = await _userManager
                                                    .AddLoginAsync(applicationUser, externalLoginInfo);

                if (addExternalLoginToCreatedUser.Succeeded)
                {
                    await _signInManager.SignInAsync(applicationUser, false);

                    await _signInManager.UpdateExternalAuthenticationTokensAsync(externalLoginInfo);

                    return(RedirectToAction(nameof(Index), "Home"));
                }
            }

            AddModelStateErrors(createdUser);

            return(View(model));
        }
예제 #4
0
        public async Task <IHttpActionResult> ExternalLoginConfirmation(ExternalLoginConfirmation model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(Ok("Redirect Index Manage"));
            }

            if (ModelState.IsValid)
            {
                // Pegar a informação do login externo.
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();

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

                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await _AppUserManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await _AppUserManager.AddLoginAsync(user.Id, info.Login);

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

                        return(BadRequest(returnUrl));
                    }
                }
                return(GetErrorResult(result));
            }

            //var returnUrl = returnUrl;
            return(BadRequest(ModelState));
        }
예제 #5
0
        public async Task <IActionResult> ExternalLoginConfirmation(ExternalLoginConfirmation model, string returnUrl = null)
        {
            if (_accessor.ActionContext.ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await _signInManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    return(new OkObjectResult("ExternalLoginFailure"));
                }
                var user = new User {
                    UserName = model.Email, Email = model.Email
                };
                var result = await _userManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await _userManager.AddLoginAsync(user, info);

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

                        _logger.LogInformation(6, "User created an account using {Name} provider.", info.LoginProvider);

                        // Update any authentication tokens as well
                        await _signInManager.UpdateExternalAuthenticationTokensAsync(info);

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

            ViewData["ReturnUrl"] = returnUrl;
            return(new OkObjectResult(model));
        }
예제 #6
0
파일: UserApp.cs 프로젝트: HOMEFW/FWS
        public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmation model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Index", "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.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    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));
        }
예제 #7
0
        public void ExternalLoginConfirmationView_Template_NullParameter_Test()
        {
            var template = new ExternalLoginConfirmation(null);

            Assert.Throws <NullReferenceException>(() => template.TransformText());
        }
예제 #8
0
        private void TransformViewsAccountExternalLoginConfirmation(SmartAppInfo manifest)
        {
            var template = new ExternalLoginConfirmation(manifest);

            _writingService.WriteFile(Path.Combine(_context.BasePath, template.OutputPath), template.TransformText());
        }