Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

appsettings.json configuration is not taken into account #15311

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Microsoft.Extensions.Configuration;
using OrchardCore.Email;
using OrchardCore.Environment.Shell.Configuration;
Expand All @@ -6,6 +7,7 @@ namespace Microsoft.Extensions.DependencyInjection
{
public static class OrchardCoreBuilderExtensions
{
[Obsolete]
public static OrchardCoreBuilder ConfigureEmailSettings(this OrchardCoreBuilder builder)
{
builder.ConfigureServices((tenantServices, serviceProvider) =>
Expand All @@ -17,5 +19,15 @@ public static OrchardCoreBuilder ConfigureEmailSettings(this OrchardCoreBuilder

return builder;
}

public static IServiceCollection ConfigureEmailSettings(this IServiceCollection services)
{
services.AddOptions<SmtpSettings>()
.Configure<IShellConfiguration>(
(options, shellConfiguration) => shellConfiguration.GetSection("OrchardCore_Email").Bind(options)
);

return services;
}
}
}
2 changes: 2 additions & 0 deletions src/OrchardCore.Modules/OrchardCore.Email/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public Startup(IOptions<AdminOptions> adminOptions)

public override void ConfigureServices(IServiceCollection services)
{
services.ConfigureEmailSettings();

services.AddScoped<IPermissionProvider, Permissions>();
services.AddScoped<IDisplayDriver<ISite>, SmtpSettingsDisplayDriver>();
services.AddScoped<INavigationProvider, AdminMenu>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Microsoft.Extensions.Configuration;
using OrchardCore.Environment.Shell.Configuration;
using OrchardCore.Facebook.Settings;
Expand All @@ -6,6 +7,7 @@ namespace Microsoft.Extensions.DependencyInjection
{
public static class OrchardCoreBuilderExtensions
{
[Obsolete]
public static OrchardCoreBuilder ConfigureFacebookSettings(this OrchardCoreBuilder builder)
{
builder.ConfigureServices((tenantServices, serviceProvider) =>
Expand All @@ -17,5 +19,15 @@ public static OrchardCoreBuilder ConfigureFacebookSettings(this OrchardCoreBuild

return builder;
}

public static IServiceCollection ConfigureFacebookSettings(this IServiceCollection services)
{
services.AddOptions<FacebookSettings>()
.Configure<IShellConfiguration>(
(options, shellConfiguration) => shellConfiguration.GetSection("OrchardCore_Facebook").Bind(options)
);

return services;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Facebook;
using Microsoft.AspNetCore.DataProtection;
Expand All @@ -20,25 +17,28 @@ public class FacebookLoginConfiguration :
IConfigureNamedOptions<FacebookOptions>
{
private readonly FacebookSettings _facebookSettings;
private readonly FacebookLoginSettings _facebookLoginSettings;
private readonly IFacebookLoginService _loginService;
private readonly IDataProtectionProvider _dataProtectionProvider;
private readonly ILogger _logger;

public FacebookLoginConfiguration(
IOptions<FacebookSettings> facebookSettings,
IOptions<FacebookLoginSettings> facebookLoginSettings,
IFacebookLoginService loginService,
IDataProtectionProvider dataProtectionProvider,
ILogger<FacebookLoginConfiguration> logger)
{
_facebookSettings = facebookSettings.Value;
_facebookLoginSettings = facebookLoginSettings.Value;
_loginService = loginService;
_dataProtectionProvider = dataProtectionProvider;
_logger = logger;
}

public void Configure(AuthenticationOptions options)
{
if (_facebookSettings == null)
if (_facebookSettings == null || _facebookLoginSettings == null)
{
return;
}
Expand All @@ -50,12 +50,6 @@ public void Configure(AuthenticationOptions options)
return;
}

var loginSettings = GetFacebookLoginSettingsAsync().GetAwaiter().GetResult();
if (loginSettings == null)
{
return;
}

// Register the OpenID Connect client handler in the authentication handlers collection.
options.AddScheme(FacebookDefaults.AuthenticationScheme, builder =>
{
Expand All @@ -72,13 +66,7 @@ public void Configure(string name, FacebookOptions options)
return;
}

if (_facebookSettings == null)
{
return;
}

var loginSettings = GetFacebookLoginSettingsAsync().GetAwaiter().GetResult();
if (loginSettings == null)
if (_facebookSettings == null || _facebookLoginSettings == null)
{
return;
}
Expand All @@ -94,27 +82,14 @@ public void Configure(string name, FacebookOptions options)
_logger.LogError("The Facebook secret key could not be decrypted. It may have been encrypted using a different key.");
}

if (loginSettings.CallbackPath.HasValue)
if (_facebookLoginSettings.CallbackPath.HasValue)
{
options.CallbackPath = loginSettings.CallbackPath;
options.CallbackPath = _facebookLoginSettings.CallbackPath;
}

options.SaveTokens = loginSettings.SaveTokens;
options.SaveTokens = _facebookLoginSettings.SaveTokens;
}

public void Configure(FacebookOptions options) => Debug.Fail("This infrastructure method shouldn't be called.");

private async Task<FacebookLoginSettings> GetFacebookLoginSettingsAsync()
{
var settings = await _loginService.GetSettingsAsync();
if ((await _loginService.ValidateSettingsAsync(settings)).Any(result => result != ValidationResult.Success))
{
_logger.LogWarning("The Facebook Login module is not correctly configured.");

return null;
}

return settings;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OrchardCore.Environment.Shell;
Expand All @@ -26,11 +25,11 @@ public FacebookSettingsConfiguration(

public void Configure(FacebookSettings options)
{
var settings = GetFacebookSettingsAsync()
var settings = _facebookService.GetSettingsAsync()
.GetAwaiter()
.GetResult();

if (settings != null)
if (IsSettingsValid(settings))
{
options.AppId = settings.AppId;
options.AppSecret = settings.AppSecret;
Expand All @@ -39,22 +38,26 @@ public void Configure(FacebookSettings options)
options.FBInitParams = settings.FBInitParams;
options.SdkJs = settings.SdkJs;
}
else
{
if (!IsSettingsValid(options))
{
_logger.LogWarning("Facebook is not correctly configured.");
}
}
}

private async Task<FacebookSettings> GetFacebookSettingsAsync()
private bool IsSettingsValid(FacebookSettings settings)
{
var settings = await _facebookService.GetSettingsAsync();

if (_facebookService.ValidateSettings(settings).Any(result => result != ValidationResult.Success))
{
if (_shellSettings.IsRunning())
{
_logger.LogWarning("Facebook is not correctly configured.");
return false;
}

return null;
}

return settings;
return true;
}
}
2 changes: 2 additions & 0 deletions src/OrchardCore.Modules/OrchardCore.Facebook/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public override void Configure(IApplicationBuilder builder, IEndpointRouteBuilde

public override void ConfigureServices(IServiceCollection services)
{
services.ConfigureFacebookSettings();

services.AddScoped<IPermissionProvider, Permissions>();
services.AddScoped<INavigationProvider, AdminMenu>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OrchardCore.Environment.Shell;
Expand All @@ -27,33 +26,36 @@ public GitHubAuthenticationSettingsConfiguration(

public void Configure(GitHubAuthenticationSettings options)
{
var settings = GetGitHubAuthenticationSettingsAsync()
var settings = _gitHubAuthenticationService.GetSettingsAsync()
.GetAwaiter()
.GetResult();

if (settings != null)
if (IsSettingsValid(settings))
{
options.CallbackPath = settings.CallbackPath;
options.ClientID = settings.ClientID;
options.ClientSecret = settings.ClientSecret;
options.SaveTokens = settings.SaveTokens;
}
else
{
if (!IsSettingsValid(options))
{
_logger.LogWarning("GitHub Authentication is not correctly configured.");
}
}
}

private async Task<GitHubAuthenticationSettings> GetGitHubAuthenticationSettingsAsync()
private bool IsSettingsValid(GitHubAuthenticationSettings settings)
{
var settings = await _gitHubAuthenticationService.GetSettingsAsync();

if ((_gitHubAuthenticationService.ValidateSettings(settings)).Any(result => result != ValidationResult.Success))
if (_gitHubAuthenticationService.ValidateSettings(settings).Any(result => result != ValidationResult.Success))
{
if (_shellSettings.IsRunning())
{
_logger.LogWarning("GitHub Authentication is not correctly configured.");
return false;
}

return null;
}

return settings;
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Microsoft.Extensions.Configuration;
using OrchardCore.Environment.Shell.Configuration;
using OrchardCore.GitHub.Settings;
Expand All @@ -6,6 +7,7 @@ namespace Microsoft.Extensions.DependencyInjection
{
public static class OrchardCoreBuilderExtensions
{
[Obsolete]
public static OrchardCoreBuilder ConfigureGitHubSettings(this OrchardCoreBuilder builder)
{
builder.ConfigureServices((tenantServices, serviceProvider) =>
Expand All @@ -17,5 +19,15 @@ public static OrchardCoreBuilder ConfigureGitHubSettings(this OrchardCoreBuilder

return builder;
}

public static IServiceCollection ConfigureGitHubSettings(this IServiceCollection services)
{
services.AddOptions<GitHubAuthenticationSettings>()
.Configure<IShellConfiguration>(
(options, shellConfiguration) => shellConfiguration.GetSection("OrchardCore_GitHub").Bind(options)
);

return services;
}
}
}
2 changes: 2 additions & 0 deletions src/OrchardCore.Modules/OrchardCore.GitHub/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class GitHubLoginStartup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
{
services.ConfigureGitHubSettings();

services.AddSingleton<IGitHubAuthenticationService, GitHubAuthenticationService>();
services.AddScoped<IDisplayDriver<ISite>, GitHubAuthenticationSettingsDisplayDriver>();
services.AddScoped<INavigationProvider, AdminMenuGitHubLogin>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OrchardCore.Environment.Shell;
Expand All @@ -26,33 +25,36 @@ public GoogleAuthenticationSettingsConfiguration(

public void Configure(GoogleAuthenticationSettings options)
{
var settings = GetGoogleAuthenticationSettingsAsync()
var settings = _googleAuthenticationService.GetSettingsAsync()
.GetAwaiter()
.GetResult();

if (settings != null)
if (IsSettingsValid(settings))
{
options.CallbackPath = settings.CallbackPath;
options.ClientID = settings.ClientID;
options.ClientSecret = settings.ClientSecret;
options.SaveTokens = settings.SaveTokens;
}
else
{
if (!IsSettingsValid(options))
{
_logger.LogWarning("Google Authentication is not correctly configured.");
}
}
}

private async Task<GoogleAuthenticationSettings> GetGoogleAuthenticationSettingsAsync()
private bool IsSettingsValid(GoogleAuthenticationSettings settings)
{
var settings = await _googleAuthenticationService.GetSettingsAsync();

if ((_googleAuthenticationService.ValidateSettings(settings)).Any(result => result != ValidationResult.Success))
if (_googleAuthenticationService.ValidateSettings(settings).Any(result => result != ValidationResult.Success))
{
if (_shellSettings.IsRunning())
{
_logger.LogWarning("Google Authentication is not correctly configured.");
return false;
}

return null;
}

return settings;
return true;
}
}
Loading
Loading