Copied to clipboard

Flag this post as spam?

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


  • kevinewig 2 posts 72 karma points
    Nov 05, 2021 @ 03:07
    kevinewig
    0

    Backoffice Azure AD Login: Unable to Compile Startup.cs

    Hi

    I am trying to have users log into the Umbraco 9 BackOffice with Azure AD. I tried to follow the post () however I have been unable to compile the code there.

    This is the error I am getting:

    Error CS1061 'BackOfficeExternalLoginProviderOptions' does not contain a definition for 'SchemeForBackOffice' and no accessible extension method 'SchemeForBackOffice' accepting a first argument of type 'BackOfficeExternalLoginProviderOptions' could be found (are you missing a using directive or an assembly reference?)

    It seems to have problems compiling auth.SchemeForBackOffice

    I have .Net Core 5.

    The libraries in my project are:

    • Microsoft.AspNetCore.Authentication.Abstractions, Version="2.2.0"
    • Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="5.0.11
    • Microsoft.AspNetCore.Authentication.Core" Version="2.2.0"
    • Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.11"
    • Microsoft.ICU.ICU4C.Runtime" Version="68.2.0.6"
    • Microsoft.Identity.Web" Version="1.19.0"

    My Startup.cs file looks like this:

    using System;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;
    using Umbraco.Cms.Core.DependencyInjection;
    using Umbraco.Cms.Web.BackOffice.Security;
    using Umbraco.Extensions;
    using Microsoft.Identity.Web;
    
    namespace MyProject
    {
        public class Startup
        {
            private readonly IWebHostEnvironment _env;
            private readonly IConfiguration _config;
    
            /// <summary>
            /// Initializes a new instance of the <see cref="Startup" /> class.
            /// </summary>
            /// <param name="webHostEnvironment">The web hosting environment.</param>
            /// <param name="config">The configuration.</param>
            /// <remarks>
            /// Only a few services are possible to be injected here https://github.com/dotnet/aspnetcore/issues/9337
            /// </remarks>
            public Startup(IWebHostEnvironment webHostEnvironment, IConfiguration config)
            {
                _env = webHostEnvironment ?? throw new ArgumentNullException(nameof(webHostEnvironment));
                _config = config ?? throw new ArgumentNullException(nameof(config));
            }
    
            /// <summary>
            /// Configures the services.
            /// </summary>
            /// <param name="services">The services.</param>
            /// <remarks>
            /// This method gets called by the runtime. Use this method to add services to the container.
            /// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
            /// </remarks>
            public void ConfigureServices(IServiceCollection services)
            {
    #pragma warning disable IDE0022 // Use expression body for methods
                services.AddUmbraco(_env, _config)
                    .AddBackOffice()
                    .AddWebsite()
                    .AddComposers()
                    .AddBackOfficeExternalLogins(extLoginBuilder =>
                    {
                        var extLoginOpts = new ExternalSignInAutoLinkOptions(
                            autoLinkExternalAccount: true,
                            defaultUserGroups: new[] { "admin" },
                            defaultCulture: "en-US",
                            allowManualLinking: true)
                        {
                            OnExternalLogin = (user, loginInfo) =>
                            {
                                return true;
                            },
                        };
    
                        var loginProviderOptions = new BackOfficeExternalLoginProviderOptions(
                            "btn-microsoft",
                            "fa-windows",
                            extLoginOpts,
                            autoRedirectLoginToExternalProvider: false);
    
                        extLoginBuilder.AddBackOfficeLogin(
                            loginProviderOptions,
                            auth =>
                            {
                                var azAdConfig = _config.GetSection("AzureAd");
    
                                auth
                    // https://github.com/umbraco/Umbraco-CMS/pull/9470
                    .AddMicrosoftIdentityWebApp(options =>
                                {
                                    options.CallbackPath = "/umbraco-signin-oidc";
                                    options.Instance = "https://login.microsoftonline.com/";
                                    options.TenantId = azAdConfig["TenantId"];
                                    options.ClientId = azAdConfig["ClientId"];
                                    options.SignedOutRedirectUri = "/umbraco";
    
                    // https://github.com/AzureAD/microsoft-identity-web/issues/749
                    //options.ClaimActions.MapJsonKey(ClaimTypes.Email, ClaimConstants.PreferredUserName);
    
                    // Preferred over IClaimsTransformation which runs for every AuthenticateAsync
                    options.Events.OnTokenValidated = ctx =>
                                    {
                                        var username = ctx.Principal?.Claims.FirstOrDefault(c => c.Type == ClaimConstants.PreferredUserName);
                                        if (username != null && ctx.Principal?.Identity is ClaimsIdentity claimsIdentity)
                                        {
                                            claimsIdentity.AddClaim(
                                                new Claim(
                                                    ClaimTypes.Email,
                                                    username.Value
                                                )
                                            );
                                        }
    
                                        return Task.CompletedTask;
                                    };
                                },
                    openIdConnectScheme: auth.SchemeForBackOffice(Constants.AzureAd),
                    cookieScheme: "Fake")
                    ;
                            });
                    })
                    .Build();
    #pragma warning restore IDE0022 // Use expression body for methods
    
            }
    
            /// <summary>
            /// Configures the application.
            /// </summary>
            /// <param name="app">The application builder.</param>
            /// <param name="env">The web hosting environment.</param>
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
    
                app.UseUmbraco()
                    .WithMiddleware(u =>
                    {
                        u.UseBackOffice();
                        u.UseWebsite();
                    })
                    .WithEndpoints(u =>
                    {
                        u.UseInstallerEndpoints();
                        u.UseBackOfficeEndpoints();
                        u.UseWebsiteEndpoints();
                    });
            }
        }
    }
    

    I would appreciate any help anyone could provide for me.

    Thank you!

    Regards, Kevin

  • Thomas 255 posts 525 karma points c-trib
    19 days ago
    Thomas
    0

    Did you get it to work?

  • kevinewig 2 posts 72 karma points
    1 week ago
    kevinewig
    0

    No. I was not able to get it to work.

    I need help authenticating Umbraco 9 with Azure AD. Any examples would be greatly appreciated.

Please Sign in or register to post replies

Write your reply to:

Draft