Skip to content

Commit

Permalink
EmailSender as a plugin and remove dependency on Microsoft.AspNetCore…
Browse files Browse the repository at this point in the history
….Identity.UI package.
  • Loading branch information
Nfactor26 committed Feb 27, 2022
1 parent 6e693de commit 6b65dce
Show file tree
Hide file tree
Showing 185 changed files with 53,157 additions and 117 deletions.
14 changes: 14 additions & 0 deletions Pixel.Identity.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pixel.Identity.Store.Sql",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pixel.Identity.Core", "src\Pixel.Identity.Core\Pixel.Identity.Core.csproj", "{E1581674-E28D-429B-B607-22EC81E44E89}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pixel.Identity.Messenger.Email", "src\Pixel.Identity.Messenger.Email\Pixel.Identity.Messenger.Email.csproj", "{E7F60792-6D88-40BB-A4F5-F70CC6B81396}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -111,6 +113,18 @@ Global
{E1581674-E28D-429B-B607-22EC81E44E89}.Release|x64.Build.0 = Release|Any CPU
{E1581674-E28D-429B-B607-22EC81E44E89}.Release|x86.ActiveCfg = Release|Any CPU
{E1581674-E28D-429B-B607-22EC81E44E89}.Release|x86.Build.0 = Release|Any CPU
{E7F60792-6D88-40BB-A4F5-F70CC6B81396}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E7F60792-6D88-40BB-A4F5-F70CC6B81396}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E7F60792-6D88-40BB-A4F5-F70CC6B81396}.Debug|x64.ActiveCfg = Debug|Any CPU
{E7F60792-6D88-40BB-A4F5-F70CC6B81396}.Debug|x64.Build.0 = Debug|Any CPU
{E7F60792-6D88-40BB-A4F5-F70CC6B81396}.Debug|x86.ActiveCfg = Debug|Any CPU
{E7F60792-6D88-40BB-A4F5-F70CC6B81396}.Debug|x86.Build.0 = Debug|Any CPU
{E7F60792-6D88-40BB-A4F5-F70CC6B81396}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E7F60792-6D88-40BB-A4F5-F70CC6B81396}.Release|Any CPU.Build.0 = Release|Any CPU
{E7F60792-6D88-40BB-A4F5-F70CC6B81396}.Release|x64.ActiveCfg = Release|Any CPU
{E7F60792-6D88-40BB-A4F5-F70CC6B81396}.Release|x64.Build.0 = Release|Any CPU
{E7F60792-6D88-40BB-A4F5-F70CC6B81396}.Release|x86.ActiveCfg = Release|Any CPU
{E7F60792-6D88-40BB-A4F5-F70CC6B81396}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
10 changes: 10 additions & 0 deletions src/Pixel.Identity.Core/Contracts/IEmailSender.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Pixel.Identity.Core
{
/// <summary>
/// IEmailSender is used for sending messages through email
/// </summary>
public interface IEmailSender
{
Task SendEmailAsync(string email, string subject, string htmlMessage);
}
}
1 change: 0 additions & 1 deletion src/Pixel.Identity.Core/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.WebUtilities;
using Pixel.Identity.Shared.Models;
Expand Down
1 change: 0 additions & 1 deletion src/Pixel.Identity.Core/Pages/ExternalLogin.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.WebUtilities;
Expand Down
3 changes: 1 addition & 2 deletions src/Pixel.Identity.Core/Pages/Register.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.WebUtilities;
Expand All @@ -10,7 +9,7 @@
using System.Text.Encodings.Web;

namespace Pixel.Identity.Core.Pages
{
{
[AllowAnonymous]
public abstract class RegisterModel : PageModel {

Expand Down
3 changes: 1 addition & 2 deletions src/Pixel.Identity.Core/Pixel.Identity.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="10.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="6.0.1" />
<PackageReference Include="AutoMapper" Version="10.1.1" />
<PackageReference Include="OpenIddict.AspNetCore" Version="3.1.1" />
</ItemGroup>

Expand Down
File renamed without changes.
7 changes: 7 additions & 0 deletions src/Pixel.Identity.Core/Plugins/IServicePlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Pixel.Identity.Core.Plugins
{
public interface IServicePlugin
{
void ConfigureService(IServiceCollection services);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using MailKit.Net.Smtp;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.Extensions.Configuration;
using MimeKit;
using MimeKit.Text;
using System.Threading.Tasks;
using Pixel.Identity.Core;

namespace Pixel.Identity.Provider
namespace Pixel.Identity.Messenger.Email
{
public class EmailSender : IEmailSender
{
Expand All @@ -31,7 +30,6 @@ public async Task SendEmailAsync(string sendTo, string subject, string htmlMessa
await smtp.SendAsync(email);
await smtp.DisconnectAsync(true);
}

}
}
}
14 changes: 14 additions & 0 deletions src/Pixel.Identity.Messenger.Email/EmailSenderPlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.Extensions.DependencyInjection;
using Pixel.Identity.Core;
using Pixel.Identity.Core.Plugins;

namespace Pixel.Identity.Messenger.Email
{
public class EmailSenderPlugin : IServicePlugin
{
public void ConfigureService(IServiceCollection services)
{
services.AddTransient<IEmailSender, EmailSender>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EnableDynamicLoading>true</EnableDynamicLoading>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MailKit" Version="3.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Pixel.Identity.Core\Pixel.Identity.Core.csproj">
<Private>false</Private>
<ExcludeAssets>runtime</ExcludeAssets>
</ProjectReference>
</ItemGroup>

</Project>
33 changes: 3 additions & 30 deletions src/Pixel.Identity.Provider/Areas/Identity/Pages/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,7 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<base href="~/" />
<environment include="Development">
<link rel="stylesheet" href="~/Identity/lib/bootstrap/dist/css/bootstrap.css" />
<link rel="stylesheet" href="~/Identity/css/site.css" />
</environment>
<environment exclude="Development">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
asp-fallback-href="~/Identity/lib/bootstrap/dist/css/bootstrap.min.css"
asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" />
<link rel="stylesheet" href="~/Identity/css/site.css" asp-append-version="true" />
</environment>
<link rel="stylesheet" href="~/lib/twitter-bootstrap/css/bootstrap.min.css" />
<link href="_content/MudBlazor/MudBlazor.min.css" rel="stylesheet" />
<component type="typeof(HeadOutlet)" render-mode="Static" />
</head>
Expand All @@ -32,26 +23,8 @@
</div>
</div>
</div>
<environment include="Development">
<script src="~/Identity/lib/jquery/dist/jquery.js"></script>
<script src="~/Identity/lib/bootstrap/dist/js/bootstrap.bundle.js"></script>
<script src="~/Identity/js/site.js" asp-append-version="true"></script>
</environment>
<environment exclude="Development">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"
asp-fallback-src="~/Identity/lib/jquery/dist/jquery.min.js"
asp-fallback-test="window.jQuery"
crossorigin="anonymous"
integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=">
</script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js"
asp-fallback-src="~/Identity/lib/bootstrap/dist/js/bootstrap.bundle.min.js"
asp-fallback-test="window.jQuery && window.jQuery.fn && window.jQuery.fn.modal"
crossorigin="anonymous"
integrity="sha384-xrRywqdh3PHs8keKZN+8zzc5TX0GRTLCcmivcbNJWm2rs5C8PRhcEn3czEjhAO9o">
</script>
<script src="~/Identity/js/site.js" asp-append-version="true"></script>
</environment>
<script src="~/lib/jquery/jquery.min.js"></script>
<script src="~/lib/twitter-bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="_framework/blazor.server.js"></script>
<script src="_content/MudBlazor/MudBlazor.min.js"></script>
@await RenderSectionAsync("Scripts", required:false)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<script src="~/lib/jquery-validation/jquery.validate.min.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
32 changes: 31 additions & 1 deletion src/Pixel.Identity.Provider/Extensions/ServiceExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using Microsoft.AspNetCore.HttpLogging;
using McMaster.NETCore.Plugins;
using Microsoft.AspNetCore.HttpLogging;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Pixel.Identity.Core.Plugins;
using System;
using System.IO;
using System.Linq;

namespace Pixel.Identity.Provider.Extensions
{
Expand Down Expand Up @@ -32,5 +37,30 @@ public static IServiceCollection ConfigureHttpLogging(this IServiceCollection se
logging.ResponseBodyLogLimit = 4096;
});
}

/// <summary>
/// Load a service plugin from plugins directory and invoke the ConfigureService() method on plugin.
/// This will allow plugin to register the required services
/// </summary>
/// <param name="services"></param>
/// <param name="pluginName"></param>
/// <param name="sharedTypes"></param>
/// <returns></returns>
public static IServiceCollection AddPlugin(this IServiceCollection services, string pluginType, string pluginName, Type[] sharedTypes)
{
string pluginsDirectory = Path.Combine(AppContext.BaseDirectory, "Plugins", "", pluginType, pluginName);
if (Directory.Exists(pluginsDirectory))
{
var pluginFile = Directory.GetFiles(pluginsDirectory, "*.dll").Where(f => Path.GetFileNameWithoutExtension(f).Equals(pluginName)).Single();
var loader = PluginLoader.CreateFromAssemblyFile(pluginFile, sharedTypes: sharedTypes);
foreach (var type in loader.LoadDefaultAssembly().GetTypes()
.Where(t => typeof(IServicePlugin).IsAssignableFrom(t) && !t.IsAbstract))
{
var servicePlugin = (IServicePlugin)Activator.CreateInstance(type);
servicePlugin.ConfigureService(services);
}
}
return services;
}
}
}
19 changes: 11 additions & 8 deletions src/Pixel.Identity.Provider/Pixel.Identity.Provider.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
<PackageReference Include="Microsoft.AspNetCore.Authentication.Negotiate" Version="6.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="6.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="6.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.1" />
<PackageReference Include="OpenIddict.AspNetCore" Version="3.1.1" />
<PackageReference Include="OpenIddict.Quartz" Version="3.1.1" />
<PackageReference Include="Quartz.Extensions.Hosting" Version="3.3.3" />
Expand All @@ -37,20 +36,24 @@

<Target Name="CopyPluginsOnPublish" AfterTargets="Publish">
<ItemGroup>
<PluginFiles Include="$(TargetDir)\Plugins\**\*.*"/>
<PluginFiles Include="$(TargetDir)\Plugins\**\*.*" />
</ItemGroup>
<!--<Message Importance="high" Text="Copy Plguins directory from $(TargetDir)\Plugins to $(PublishDir)" />-->
<MakeDir Directories="$(PublishDir)\Plugins"/>
<Copy SourceFiles="@(PluginFiles)" DestinationFiles="@(PluginFiles -> '$(PublishDir)Plugins\%(RecursiveDir)%(Filename)%(Extension)')"/>
<MakeDir Directories="$(PublishDir)\Plugins" />
<Copy SourceFiles="@(PluginFiles)" DestinationFiles="@(PluginFiles -> '$(PublishDir)Plugins\%(RecursiveDir)%(Filename)%(Extension)')" />
</Target>

<Target Name="PublishPluginsOnBuild" BeforeTargets="Build">
<ItemGroup>
<PluginProject Include="..\Pixel.Identity.Store.Mongo\Pixel.Identity.Store.Mongo.csproj" />
<PluginProject Include="..\Pixel.Identity.Store.Sql\Pixel.Identity.Store.Sql.csproj" />
<DbStorePluginProject Include="..\Pixel.Identity.Store.Mongo\Pixel.Identity.Store.Mongo.csproj" />
<DbStorePluginProject Include="..\Pixel.Identity.Store.Sql\Pixel.Identity.Store.Sql.csproj" />
</ItemGroup>
<ItemGroup>
<MessengerPluginProject Include="..\Pixel.Identity.Messenger.Email\Pixel.Identity.Messenger.Email.csproj" />
</ItemGroup>
<!--<Message Importance="high" Text="Executing build plugins now with PublishDir $(TargetDir)Plugins" />-->
<MSBuild Projects="@(PluginProject)" Targets="Publish" Properties="PublishDir=$(TargetDir)Plugins\DbStore\%(FileName)\" />
<MSBuild Projects="@(DbStorePluginProject)" Targets="Publish" Properties="PublishDir=$(TargetDir)Plugins\DbStore\%(FileName)\" />
<MSBuild Projects="@(MessengerPluginProject)" Targets="Publish" Properties="PublishDir=$(TargetDir)Plugins\Messenger\%(FileName)\" />
</Target>

</Project>
43 changes: 23 additions & 20 deletions src/Pixel.Identity.Provider/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
using McMaster.NETCore.Plugins;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using MudBlazor;
using MudBlazor.Services;
using Pixel.Identity.Core;
using Pixel.Identity.Provider.Extensions;
using Pixel.Identity.Shared;
using Quartz;
using System;
Expand Down Expand Up @@ -59,16 +58,7 @@ public void ConfigureServices(IServiceCollection services)
{
c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
});

var authenticationBuilder = services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
});
foreach(var externalProvider in LoadExternalProviderPlugins())
{
externalProvider.AddProvider(this.Configuration, authenticationBuilder);
}


services.AddMudServices(config =>
{
config.SnackbarConfiguration.PositionClass = Defaults.Classes.Position.TopRight;
Expand All @@ -81,15 +71,24 @@ public void ConfigureServices(IServiceCollection services)
});

ConfigureCors(services);


var authenticationBuilder = services.AddAuthentication();
foreach (var externalProvider in LoadExternalProviderPlugins())
{
externalProvider.AddProvider(this.Configuration, authenticationBuilder);
}

ConfigureOpenIddict(services, dbStorePlugin);

ConfigureAuthorizationPolicy(services);

ConfigureQuartz(services);
ConfigureQuartz(services);

services.AddPlugin("Messenger", Configuration["EmailSenderPlugin"], new[]
{
typeof(IConfiguration), typeof(IEmailSender)
});

services.AddTransient<IEmailSender, EmailSender>();

dbStorePlugin.AddServices(services);
}

Expand Down Expand Up @@ -166,8 +165,7 @@ private IConfigurator LoadDbStorePlugin()
{
typeof(IConfigurator),
typeof(OpenIddictQuartzBuilder),
typeof(UI.Client.Program),
typeof(Microsoft.AspNetCore.Identity.UI.Services.IEmailSender)
typeof(UI.Client.Program)
});
foreach (var type in loader.LoadDefaultAssembly().GetTypes()
.Where(t => typeof(IConfigurator).IsAssignableFrom(t) && !t.IsAbstract))
Expand Down Expand Up @@ -275,10 +273,15 @@ private void ConfigureAuthorizationPolicy(IServiceCollection services)
/// <param name="configurator"></param>
private void ConfigureOpenIddict(IServiceCollection services, IConfigurator configurator)
{
configurator.ConfigureIdentity(this.Configuration, services)
.AddDefaultUI()
//Configure Identity will call services.AddIdentity which will AddAuthentication
configurator.ConfigureIdentity(this.Configuration, services)
.AddSignInManager()
.AddDefaultTokenProviders();

services.ConfigureApplicationCookie(opts => {
opts.LoginPath = "/Identity/Account/Login";
});

var openIdBuilder = services.AddOpenIddict()
// Register the OpenIddict server components.
.AddServer(options =>
Expand Down
Loading

0 comments on commit 6b65dce

Please sign in to comment.