Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • tuobaling 4 posts 34 karma points
    Mar 14, 2022 @ 11:09
    tuobaling
    0

    Umbraco 9 google oauth problem

    Hello,

    I tried to add google login verification to the umbraco backoffice login page, but the problem of redirection failed.

    After I clicked to log in with a google account, an error message from google appeared on the page, error 400. After checking for a long time, I really I don't know what I did wrong, so I came up and asked, I hope you can help, thank you.

    The following are the parameters and programs I set. I wrote two programs, GoogleBackofficeAuthenticationExtensions.cs and GoogleBackOfficeExternalLoginProviderOptions.cs.

    enter image description here

    enter image description here

    enter image description here

    enter image description here

    enter image description here

    enter image description here

    using Umbraco.Cms.Core.DependencyInjection;
    using Umbraco.Extensions;
    using Umbraco.Cms.Web.BackOffice.Security;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Configuration;
    
    namespace UmbracoDemo1.OAuth
    {
        public static class GoogleBackofficeAuthenticationExtensions
        {
            public static IUmbracoBuilder AddGoogleBackofficeAuthentication(this IUmbracoBuilder builder)
            {
                builder.AddBackOfficeExternalLogins(logins =>
                {
                    logins.AddBackOfficeLogin(
                        backOfficeAuthenticationBuilder =>
                        {
                            backOfficeAuthenticationBuilder.AddGoogle(
                                // The scheme must be set with this method to work for the back office
                                backOfficeAuthenticationBuilder.SchemeForBackOffice(GoogleBackOfficeExternalLoginProviderOptions.SchemeName),
                                options =>
                                {
                                    ////只有 OAuth 同意畫面中列出的測試使用者具備 OAuth 存取權限,名單如下:
                                    ////[email protected]
                                    ////  By default this is '/signin-google' but it needs to be changed to this
                                    IConfigurationSection googleAuthNSection = builder.Config.GetSection("Authentication:Google");
                                    options.CallbackPath = googleAuthNSection["CallbackPath"];
                                    options.ClientId = googleAuthNSection["ClientId"];
                                    options.ClientSecret = googleAuthNSection["ClientSecret"];
                                });
                        });
                });
                return builder;
                //builder.AddBackOfficeExternalLogins(loginOptions =>
                //    loginOptions.AddBackOfficeLogin(new BackOfficeExternalLoginProviderOptions(
                //        "btn-success",
                //        "fa-google"
                //    ), authOptions => authOptions.AddGoogle("Umbraco.Google","Google", options =>
                //    {
                //        IConfigurationSection googleAuthNSection = builder.Config.GetSection("Authentication:Google");
                //        options.ClientId = googleAuthNSection["ClientId"];
                //        options.ClientSecret = googleAuthNSection["ClientSecret"];
                //    })));
                //return builder;
            }
        }
    }
    

    using Microsoft.Extensions.Options;
    using Umbraco.Cms.Core;
    using Umbraco.Cms.Web.BackOffice.Security;
    
    namespace UmbracoDemo1.OAuth
    {
        public class GoogleBackOfficeExternalLoginProviderOptions : IConfigureNamedOptions<BackOfficeExternalLoginProviderOptions>
        {
            public const string SchemeName = "Google";
            public void Configure(string name, BackOfficeExternalLoginProviderOptions options)
            {
                if (name != "Umbraco." + SchemeName)
                {
                    return;
                }
    
                Configure(options);
            }
    
            public void Configure(BackOfficeExternalLoginProviderOptions options)
            {
                options.ButtonStyle = "btn-danger";
                options.Icon = "fa fa-cloud";
                options.AutoLinkOptions = new ExternalSignInAutoLinkOptions(
                    // must be true for auto-linking to be enabled
                    autoLinkExternalAccount: true,
    
                    // Optionally specify default user group, else
                    // assign in the OnAutoLinking callback
                    // (default is editor)
                    defaultUserGroups: new[] { Constants.Security.EditorGroupAlias },
    
                    // Optionally specify the default culture to create
                    // the user as. If null it will use the default
                    // culture defined in the web.config, or it can
                    // be dynamically assigned in the OnAutoLinking
                    // callback.
    
                    defaultCulture: null,
                    // Optionally you can disable the ability to link/unlink
                    // manually from within the back office. Set this to false
                    // if you don't want the user to unlink from this external
                    // provider.
                    allowManualLinking: false
                )
                {
                    // Optional callback
                    OnAutoLinking = (autoLinkUser, loginInfo) =>
                    {
                        // You can customize the user before it's linked.
                        // i.e. Modify the user's groups based on the Claims returned
                        // in the externalLogin info
                    },
                    OnExternalLogin = (user, loginInfo) =>
                    {
                        // You can customize the user before it's saved whenever they have
                        // logged in with the external provider.
                        // i.e. Sync the user's name based on the Claims returned
                        // in the externalLogin info
    
                        return true; //returns a boolean indicating if sign in should continue or not.
                    }
                };
    
                // Optionally you can disable the ability for users
                // to login with a username/password. If this is set
                // to true, it will disable username/password login
                // even if there are other external login providers installed.
                options.DenyLocalLogin = false;
    
                // Optionally choose to automatically redirect to the
                // external login provider so the user doesn't have
                // to click the login button. This is
                options.AutoRedirectLoginToExternalProvider = false;
            }
        }
    }
    

    enter image description here

  • Mahender Singh 39 posts 170 karma points
    May 03, 2023 @ 17:58
    Mahender Singh
    0

    Hello tuobaling,

    I am also facing the same issue. I want to create google login page for website not for Umbraco administrator. If your issue is resolved, please help me as well.

Please Sign in or register to post replies

Write your reply to:

Draft