Ejemplo n.º 1
0
        public virtual async Task <IActionResult> OnGetAsync()
        {
            LoginInput = new LoginInputModel();

            var schemes = await SchemeProvider.GetAllSchemesAsync();

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

            EnableLocalLogin = await SettingProvider.IsTrueAsync(AccountSettingNames.EnableLocalLogin);

            ExternalProviders = providers.ToArray();

            if (IsExternalLoginOnly)
            {
                //return await ExternalLogin(vm.ExternalLoginScheme, returnUrl);
                throw new NotImplementedException();
            }

            return(Page());
        }
Ejemplo n.º 2
0
        protected virtual async Task <List <ExternalProviderModel> > GetExternalProviders()
        {
            var schemes = await SchemeProvider.GetAllSchemesAsync();

            return(schemes
                   .Where(x => x.DisplayName != null || x.Name.Equals(AccountOptions.WindowsAuthenticationSchemeName, StringComparison.OrdinalIgnoreCase))
                   .Select(x => new ExternalProviderModel {
                DisplayName = x.DisplayName,
                AuthenticationScheme = x.Name
            })
                   .ToList());
        }
Ejemplo n.º 3
0
        private async Task <IEnumerable <ExternalProvider> > GetExternalLoginsAsync(string returnUrl)
        {
            var externalProviders = default(IEnumerable <ExternalProvider>);

            var context = await Interaction.GetAuthorizationContextAsync(returnUrl);

            if (context?.IdP != null && await SchemeProvider.GetSchemeAsync(context.IdP) != null)
            {
                var enableLocalLogin = context.IdP == IdentityServer4.IdentityServerConstants.LocalIdentityProvider;

                externalProviders = !enableLocalLogin
                    ? new ExternalProvider[] { new ExternalProvider {
                                                   AuthenticationScheme = context.IdP
                                               } }
                    : new ExternalProvider[] { };
            }

            externalProviders = (await SchemeProvider.GetAllSchemesAsync())
                                .Where((authenticationScheme) => authenticationScheme.DisplayName != null)
                                .Select
                                (
                (authenticationScheme) => new ExternalProvider
            {
                DisplayName          = authenticationScheme.DisplayName ?? authenticationScheme.Name,
                AuthenticationScheme = authenticationScheme.Name
            }
                                ).ToList();

            var allowLocal = true;

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

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

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

            return(externalProviders.ToArray());
        }
Ejemplo n.º 4
0
        public static string GetPictureURL(AuthenticateResult authenticateResult)
        {
            SchemeProvider schemeProvider = SchemeProvider.FromString(authenticateResult.Ticket.AuthenticationScheme);

            if (schemeProvider == SchemeProvider.Google)
            {
                return(GetGooglePictureUrl(authenticateResult.Ticket.Properties.Items[".Token.access_token"]));
            }
            else if (schemeProvider == SchemeProvider.Facebook)
            {
                return(GetFacebookPictureUrl(authenticateResult.Principal.FindFirstValue(ClaimTypes.NameIdentifier)));
            }
            else
            {
                return(string.Empty);
            }
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> LoginExternalCallbackAsync(string scheme)
        {
            AuthenticateResult authenticateResult = await HttpContext.AuthenticateAsync(scheme);

            if (authenticateResult.Succeeded)
            {
                string memberUsername = authenticateResult.Principal.FindFirstValue(ClaimTypes.Email);
                string externalID     = authenticateResult.Principal.FindFirstValue(ClaimTypes.NameIdentifier);
                string name           = authenticateResult.Principal.FindFirstValue(ClaimTypes.Name);

                string pictureUrl = PictureURLHelper.GetPictureURL(authenticateResult);

                LoginExternalRequest request = new LoginExternalRequest
                {
                    MemberUsername = memberUsername,
                    ExternalID     = externalID,
                    SchemeProvider = SchemeProvider.FromString(scheme),
                    FullName       = name,
                    PictureURL     = pictureUrl
                };

                var response = await Mediator.Send(request);

                if (!string.IsNullOrEmpty(response.Token))
                {
                    return(Redirect($"{_currentWeb.BaseURL}/account/login-external?token={response.Token}"));
                }
                else
                {
                    return(Redirect($"{_currentWeb.BaseURL}/account/login"));
                }
            }
            else
            {
                return(Redirect($"{_currentWeb.BaseURL}/account/login"));
            }
        }
Ejemplo n.º 6
0
        #pragma warning disable 1998
        public async override global::System.Threading.Tasks.Task ExecuteAsync()
        {
#line 3 "E:\PROJECT\FBGoggleLogin\FBGoggleLogin\Views\Auth\SignIn.cshtml"

            ViewBag.Title = "Sign in";

#line default
#line hidden
            BeginContext(137, 216, true);
            WriteLiteral("\r\n<div class=\"row\">\r\n    <div class=\"col-md-12\">\r\n        <h3>Login with social providers</h3>\r\n        <h4>Select from the list of available social providers asda</h4>\r\n        <div class=\"btn-group\" role=\"group\">\r\n");
            EndContext();
#line 12 "E:\PROJECT\FBGoggleLogin\FBGoggleLogin\Views\Auth\SignIn.cshtml"
            foreach (var provider in await SchemeProvider.GetRequestHandlerSchemesAsync())
            {
#line default
#line hidden
                BeginContext(461, 16, true);
                WriteLiteral("                ");
                EndContext();
                BeginContext(477, 272, false);
                __tagHelperExecutionContext = __tagHelperScopeManager.Begin("a", global::Microsoft.AspNetCore.Razor.TagHelpers.TagMode.StartTagAndEndTag, "8edf6367495c4298a63f003d495758a4", async() => {
                    BeginContext(685, 2, true);
                    WriteLiteral("<i");
                    EndContext();
                    BeginWriteAttribute("class", " class=\"", 687, "\"", 725, 3);
                    WriteAttributeValue("", 695, "fa", 695, 2, true);
                    WriteAttributeValue(" ", 697, "fa-", 698, 4, true);
#line 17 "E:\PROJECT\FBGoggleLogin\FBGoggleLogin\Views\Auth\SignIn.cshtml"
                    WriteAttributeValue("", 701, provider.Name.ToLower(), 701, 24, false);

#line default
#line hidden
                    EndWriteAttribute();
                    BeginContext(726, 5, true);
                    WriteLiteral("></i>");
                    EndContext();
                    BeginContext(732, 13, false);
#line 17 "E:\PROJECT\FBGoggleLogin\FBGoggleLogin\Views\Auth\SignIn.cshtml"
                    Write(provider.Name);

#line default
#line hidden
                    EndContext();
                }
                                                                            );
                __Microsoft_AspNetCore_Mvc_TagHelpers_AnchorTagHelper = CreateTagHelper <global::Microsoft.AspNetCore.Mvc.TagHelpers.AnchorTagHelper>();
                __tagHelperExecutionContext.Add(__Microsoft_AspNetCore_Mvc_TagHelpers_AnchorTagHelper);
                __tagHelperExecutionContext.AddHtmlAttribute(__tagHelperAttribute_0);
                if (__Microsoft_AspNetCore_Mvc_TagHelpers_AnchorTagHelper.RouteValues == null)
                {
                    throw new InvalidOperationException(InvalidTagHelperIndexerAssignment("asp-route-returnUrl", "Microsoft.AspNetCore.Mvc.TagHelpers.AnchorTagHelper", "RouteValues"));
                }
                BeginWriteTagHelperAttribute();
#line 15 "E:\PROJECT\FBGoggleLogin\FBGoggleLogin\Views\Auth\SignIn.cshtml"
                WriteLiteral(Context.Request.Query["returnUrl"]);

#line default
#line hidden
                __tagHelperStringValueBuffer = EndWriteTagHelperAttribute();
                __Microsoft_AspNetCore_Mvc_TagHelpers_AnchorTagHelper.RouteValues["returnUrl"] = __tagHelperStringValueBuffer;
                __tagHelperExecutionContext.AddTagHelperAttribute("asp-route-returnUrl", __Microsoft_AspNetCore_Mvc_TagHelpers_AnchorTagHelper.RouteValues["returnUrl"], global::Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeValueStyle.DoubleQuotes);
                BeginWriteTagHelperAttribute();
#line 16 "E:\PROJECT\FBGoggleLogin\FBGoggleLogin\Views\Auth\SignIn.cshtml"
                WriteLiteral(provider.Name);

#line default
#line hidden
                __tagHelperStringValueBuffer = EndWriteTagHelperAttribute();
                __Microsoft_AspNetCore_Mvc_TagHelpers_AnchorTagHelper.RouteValues["provider"] = __tagHelperStringValueBuffer;
                __tagHelperExecutionContext.AddTagHelperAttribute("asp-route-provider", __Microsoft_AspNetCore_Mvc_TagHelpers_AnchorTagHelper.RouteValues["provider"], global::Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeValueStyle.DoubleQuotes);
                __Microsoft_AspNetCore_Mvc_TagHelpers_AnchorTagHelper.Action = (string)__tagHelperAttribute_1.Value;
                __tagHelperExecutionContext.AddTagHelperAttribute(__tagHelperAttribute_1);
                await __tagHelperRunner.RunAsync(__tagHelperExecutionContext);

                if (!__tagHelperExecutionContext.Output.IsContentModified)
                {
                    await __tagHelperExecutionContext.SetOutputContentAsync();
                }
                Write(__tagHelperExecutionContext.Output);
                __tagHelperExecutionContext = __tagHelperScopeManager.End();
                EndContext();
                BeginContext(749, 2, true);
                WriteLiteral("\r\n");
                EndContext();
#line 18 "E:\PROJECT\FBGoggleLogin\FBGoggleLogin\Views\Auth\SignIn.cshtml"
            }

#line default
#line hidden
            BeginContext(766, 34, true);
            WriteLiteral("        </div>\r\n    </div>\r\n</div>");
            EndContext();
        }
        private async Task <RegisterViewModel> BuildRegisterViewModelAsync(string returnUrl)
        {
            var context = await Interaction.GetAuthorizationContextAsync(returnUrl);

            if (context?.IdP != null && await SchemeProvider.GetSchemeAsync(context.IdP) != null)
            {
                var local = context.IdP == IdentityServer4.IdentityServerConstants.LocalIdentityProvider;

                // this is meant to short circuit the UI and only trigger the one external IdP
                var viewModel = new RegisterViewModel
                {
                    EnableLocalLogin  = local,
                    Email             = context?.LoginHint,
                    ReturnUrl         = returnUrl,
                    ExternalProviders = !local
                        ? new [] { new ExternalProvider {
                                       AuthenticationScheme = context.IdP
                                   } }
                        : Enumerable.Empty <ExternalProvider>()
                };

                return(viewModel);
            }

            var schemes = await SchemeProvider.GetAllSchemesAsync();

            var externalProviders = schemes
                                    .Where((authenticationScheme) => authenticationScheme.DisplayName != null)
                                    .Select
                                    (
                (authenticationScheme) => new ExternalProvider
            {
                DisplayName          = authenticationScheme.DisplayName ?? authenticationScheme.Name,
                AuthenticationScheme = authenticationScheme.Name
            }
                                    ).ToList();

            var allowLocal = true;

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

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

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

            return(new RegisterViewModel
            {
                AllowRememberLogin = AccountOptions.AllowRememberLogin,
                EnableLocalLogin = allowLocal && AccountOptions.AllowLocalLogin,
                Email = context?.LoginHint,
                ReturnUrl = returnUrl,
                ExternalProviders = externalProviders.ToArray()
            });
        }
Ejemplo n.º 8
0
        /*****************************************/
        /* helper APIs for the AccountController */
        /*****************************************/
        private async Task <LoginViewModel> BuildLoginViewModelAsync(string returnUrl)
        {
            var context = await Interaction.GetAuthorizationContextAsync(returnUrl);

            if (context?.IdP != null && await SchemeProvider.GetSchemeAsync(context.IdP) != null)
            {
                var local = context.IdP == IdentityServer4.IdentityServerConstants.LocalIdentityProvider;

                // this is meant to short circuit the UI and only trigger the one external IdP
                var vm = new LoginViewModel
                {
                    EnableLocalLogin = local,
                    ReturnUrl        = returnUrl,
                    Username         = context?.LoginHint,
                };

                if (!local)
                {
                    vm.ExternalProviders = new[] { new ExternalProvider {
                                                       AuthenticationScheme = context.IdP
                                                   } };
                }

                return(vm);
            }

            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();
                    }
                }
            }

            return(new LoginViewModel
            {
                AllowRememberLogin = AccountOptions.AllowRememberLogin,
                EnableLocalLogin = allowLocal && AccountOptions.AllowLocalLogin,
                ReturnUrl = returnUrl,
                Username = context?.LoginHint,
                ExternalProviders = providers.ToArray()
            });
        }
        public override async Task <IActionResult> OnGetAsync()
        {
            LoginInput = new LoginInputModel();

            var context = await Interaction.GetAuthorizationContextAsync(ReturnUrl);

            if (context != null)
            {
                LoginInput.UserNameOrEmailAddress = context.LoginHint;

                //TODO: Reference AspNetCore MultiTenancy module and use options to get the tenant key!
                var tenant = context.Parameters[TenantResolverConsts.DefaultTenantKey];
                if (!string.IsNullOrEmpty(tenant))
                {
                    CurrentTenant.Change(Guid.Parse(tenant));
                    Response.Cookies.Append(TenantResolverConsts.DefaultTenantKey, tenant);
                }
            }

            if (context?.IdP != null)
            {
                LoginInput.UserNameOrEmailAddress = context.LoginHint;
                ExternalProviders = new[] { new ExternalProviderModel {
                                                AuthenticationScheme = context.IdP
                                            } };
                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 ExternalProviderModel
            {
                DisplayName          = x.DisplayName,
                AuthenticationScheme = x.Name
            })
                            .ToList();

            EnableLocalLogin = await SettingProvider.IsTrueAsync(AccountSettingNames.EnableLocalLogin);

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

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

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

            ExternalProviders = providers.ToArray();

            if (IsExternalLoginOnly)
            {
                return(await base.OnPostExternalLogin(providers.First().AuthenticationScheme));
            }

            return(Page());
        }