Ejemplo n.º 1
0
        public async Task <IActionResult> OnGet(string returnUrl)
        {
            var context = await _interaction.GetAuthorizationContextAsync(returnUrl);

            if (context?.IdP != null)
            {
                // if IdP is passed, then bypass showing the login screen
                return(ExternalLogin(context.IdP, returnUrl));
            }

            await BuildLoginViewModelAsync(returnUrl, context);

            if (EnableLocalLogin == false && ExternalProviders.Count() == 1)
            {
                // only one option for logging in
                return(ExternalLogin(ExternalProviders.First().AuthenticationScheme, returnUrl));
            }

            return(Page());
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> OnGetAsync(string returnUrl = null)
        {
            if (!string.IsNullOrEmpty(ErrorMessage))
            {
                ModelState.AddModelError(string.Empty, ErrorMessage);
            }

            returnUrl = returnUrl ?? Url.Content("~/");

            // Clear the existing external cookie to ensure a clean login process
            await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);

            //ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            var context = await _interaction.GetAuthorizationContextAsync(returnUrl);

            if (context?.IdP != null && await _schemeProvider.GetSchemeAsync(context.IdP) != null)
            {
                var local = context.IdP == IdentityServer4.IdentityServerConstants.LocalIdentityProvider;
                EnableLocalLogin = local;
                ReturnUrl        = returnUrl;
                Username         = context?.LoginHint;

                if (!local)
                {
                    ExternalProviders = new[] { new ExternalProvider {
                                                    AuthenticationScheme = context.IdP
                                                } };
                }
                if (EnableLocalLogin == false && ExternalProviders?.Count() == 1)
                {
                    return(RedirectToAction("Challenge", "External", new { provider = ExternalLoginScheme, returnUrl }));
                }
                return(Page());
            }

            var schemes = await _schemeProvider.GetAllSchemesAsync();

            var providers = schemes
                            .Where(x => x.DisplayName != null ||
                                   (x.Name.Equals(AccountOptions.WindowsAuthenticationSchemeName, StringComparison.OrdinalIgnoreCase))
                                   )
                            .Select(x => new ExternalProvider
            {
                DisplayName          = x.DisplayName,
                AuthenticationScheme = x.Name
            }).ToList();

            var allowLocal = true;

            if (context?.ClientId != null)
            {
                var client = await _clientStore.FindEnabledClientByIdAsync(context.ClientId);

                if (client != null)
                {
                    allowLocal = client.EnableLocalLogin;

                    if (client.IdentityProviderRestrictions != null && client.IdentityProviderRestrictions.Any())
                    {
                        providers = providers.Where(provider => client.IdentityProviderRestrictions.Contains(provider.AuthenticationScheme)).ToList();
                    }
                }
            }

            AllowRememberLogin = AccountOptions.AllowRememberLogin;
            EnableLocalLogin   = allowLocal && AccountOptions.AllowLocalLogin;
            ReturnUrl          = returnUrl;
            Username           = context?.LoginHint;
            ExternalProviders  = providers.ToArray();
            return(Page());
        }