Пример #1
0
        public async Task <IActionResult> ExternalLoginConfirmation(ExternalLoginVM model, string returnUrl = null)
        {
            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await _signInManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    throw new ApplicationException("Error loading external login information during confirmation.");
                }
                var user = new ApplicationUser {
                    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("User created an account using {Name} provider.", info.LoginProvider);
                        return(RedirectToLocal(returnUrl));
                    }
                }
                AddErrors(result);
            }

            ViewData["ReturnUrl"] = returnUrl;
            return(View(nameof(ExternalLogin), model));
        }
Пример #2
0
        public IEnumerable <ExternalLoginVM> GetExternalLogins(string returnUrl,
                                                               bool generateState = false)
        {
            var    descriptions = Authentication.GetExternalAuthenticationTypes();
            var    logins       = new List <ExternalLoginVM>();
            string state;

            if (generateState)
            {
                state = RandomOAuthStateGenerator.Generate(StrengthInBits);
            }
            else
            {
                state = null;
            }

            foreach (var description in descriptions)
            {
                var vm = new ExternalLoginVM
                {
                    Name = description.Caption,
                    Url  = Url.Route("ExternalLogin", new
                    {
                        provider      = description.AuthenticationType,
                        response_type = "token",
                        client_id     = Startup.PublicClientId,
                        redirect_uri  = new Uri(Request.RequestUri, returnUrl).AbsoluteUri,
                        state         = state
                    }),
                    State = state
                };
                logins.Add(vm);
            }

            return(logins);
        }