Hi,
I am trying to update some of our custom code for AD login, in the code we use on Version 10 and 13, we inject the user service in to the code that uses IConfigureNamedOptions but every time I do this, Umbraco will not boot.
Here is the code
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Settings.App_Code.Helpers;
using Umbraco.Cms.Api.Management.Security;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Web.Common.Controllers;
namespace UmbracoCMSAzureActiveDirectory.App_Code.AzureActiveDirectory
{
public class OpenIdConnectBackOfficeExternalLoginProviderOptions : IConfigureNamedOptions
{
public const string SchemeName = "Microsoft";
private settings Settings = new settings();
private readonly IUserService _userService;
private readonly IHttpContextAccessor _httpContext;
private IConfiguration _config;
private ILogger<OpenIdConnectBackOfficeExternalLoginProviderOptions> _logger;
public OpenIdConnectBackOfficeExternalLoginProviderOptions(IConfiguration config, ILogger<OpenIdConnectBackOfficeExternalLoginProviderOptions> logger, IUserService userService) //IUserService userService, IHttpContextAccessor httpContextAccessor, IConfiguration config
{
//_userService = userService;
//_httpContext = httpContextAccessor;
//_config = config;
_logger= logger;
_logger.Log(LogLevel.Information, "Active AD Starting......");
}
public void Configure(string? name, BackOfficeExternalLoginProviderOptions options)
{
if (name != "Umbraco." + SchemeName)
{
_logger.Log(LogLevel.Information, "Active AD Scheme Name dose not match......");
return;
}
_logger.Log(LogLevel.Information, "Active AD Scheme Name ok......");
_logger.Log(LogLevel.Information, "Active AD Scheme Name:" + "Umbraco." + SchemeName);
Configure(options);
}
public void Configure(BackOfficeExternalLoginProviderOptions options)
{
_logger.Log(LogLevel.Information, "Active AD Configing......");
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.AdminGroupAlias },
// 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
//autoLinkUser.IsApproved = true;
},
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
// check the umbraco user status
var umbracoUser = _userService.GetByEmail(user.Email);
if (umbracoUser != null)
{
// is this a valid approved umbraco user
if (umbracoUser.IsApproved && umbracoUser.IsLockedOut == false)
{
return true;
}
// is this an invited umbraco user? - set to approved automatically
else if (umbracoUser.UserState == Umbraco.Cms.Core.Models.Membership.UserState.Invited)
{
user.IsApproved = true;
return true;
}
// is this is an existing user who has previously signed in but they are disabled or locked out
else if (umbracoUser.LastLoginDate != null && (!umbracoUser.IsApproved || umbracoUser.IsLockedOut))
{
return false;
}
else if (!umbracoUser.IsApproved && umbracoUser.LastLoginDate == null)
{
// this user has never logged in and is not approved
// this must be a user that was not already set up in umbraco
// if not an existing user, delete the new record
_userService.Delete(umbracoUser, true);
return false;
}
}
return true; //returns a boolean indicating if sign in should continue or not.
}
};
options.DenyLocalLogin = false;
}
}
}