Browse Source

Introduce module naming to prevent multiple registrations

pull/41/head
Kévin Chalet 10 years ago
parent
commit
b6636d2dbc
  1. 2
      src/OpenIddict.Assets/OpenIddictExtensions.cs
  2. 13
      src/OpenIddict.Core/OpenIddictExtensions.cs
  3. 5
      src/OpenIddict.Core/OpenIddictModule.cs
  4. 2
      src/OpenIddict.Mvc/OpenIddictController.cs
  5. 7
      src/OpenIddict.Mvc/OpenIddictExtensions.cs

2
src/OpenIddict.Assets/OpenIddictExtensions.cs

@ -12,7 +12,7 @@ using Microsoft.Extensions.Internal;
namespace Microsoft.AspNet.Builder {
public static class OpenIddictExtensions {
public static OpenIddictBuilder UseAssets([NotNull] this OpenIddictBuilder builder) {
return builder.AddModule(-20, app => app.UseStaticFiles(new StaticFileOptions {
return builder.AddModule("Assets", -20, app => app.UseStaticFiles(new StaticFileOptions {
FileProvider = new EmbeddedFileProvider(
assembly: Assembly.Load(new AssemblyName("OpenIddict.Assets")),
baseNamespace: "OpenIddict.Assets")

13
src/OpenIddict.Core/OpenIddictExtensions.cs

@ -46,9 +46,16 @@ namespace Microsoft.AspNet.Builder {
}
public static OpenIddictBuilder AddModule(
[NotNull] this OpenIddictBuilder builder, int position,
[NotNull] this OpenIddictBuilder builder,
[NotNull] string name, int position,
[NotNull] Action<IApplicationBuilder> registration) {
// By default, prevent duplicate registrations.
if (builder.Modules.Any(module => string.Equals(module.Name, name))) {
return builder;
}
builder.Modules.Add(new OpenIddictModule {
Name = name,
Position = position,
Registration = registration
});
@ -74,7 +81,7 @@ namespace Microsoft.AspNet.Builder {
configuration(builder);
builder.AddModule(-10, map => map.UseCors(options => {
builder.AddModule("CORS", -10, map => map.UseCors(options => {
options.AllowAnyHeader();
options.AllowAnyMethod();
options.AllowAnyOrigin();
@ -82,7 +89,7 @@ namespace Microsoft.AspNet.Builder {
}));
// Add OpenIdConnectServerMiddleware to the ASP.NET 5 pipeline.
builder.AddModule(0, map => map.UseOpenIdConnectServer(builder.Options));
builder.AddModule("ASOS", 0, map => map.UseOpenIdConnectServer(builder.Options));
// Register the OpenIddict modules in the ASP.NET 5 pipeline.
foreach (var module in builder.Modules.OrderBy(module => module.Position)) {

5
src/OpenIddict.Core/OpenIddictModule.cs

@ -6,6 +6,11 @@ namespace OpenIddict {
/// Defines an OpenIddict module.
/// </summary>
public class OpenIddictModule {
/// <summary>
/// Gets or sets the name of the module.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets or sets the position of the module in the ASP.NET pipeline.
/// </summary>

2
src/OpenIddict.Mvc/OpenIddictController.cs

@ -18,7 +18,7 @@ using Microsoft.AspNet.Mvc;
using Microsoft.Extensions.Internal;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
namespace OpenIddict {
namespace OpenIddict.Mvc {
// Note: this controller is generic and doesn't need to be marked as internal to prevent MVC from discovering it.
public class OpenIddictController<TUser, TApplication> : Controller where TUser : class where TApplication : class {
public OpenIddictController(

7
src/OpenIddict.Mvc/OpenIddictExtensions.cs

@ -15,11 +15,12 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Internal;
using NWebsec.Middleware;
using OpenIddict;
using OpenIddict.Mvc;
namespace Microsoft.AspNet.Builder {
public static class OpenIddictExtensions {
public static OpenIddictBuilder UseMvc([NotNull] this OpenIddictBuilder builder) {
builder.AddModule(-20, app => {
builder.AddModule("NWebsec", -20, app => {
// Insert a new middleware responsible of setting the Content-Security-Policy header.
// See https://nwebsec.codeplex.com/wikipage?title=Configuring%20Content%20Security%20Policy&referringTitle=NWebsec
app.UseCsp(options => options.DefaultSources(directive => directive.Self())
@ -41,7 +42,7 @@ namespace Microsoft.AspNet.Builder {
});
// Run the rest of the pipeline in an isolated environment.
builder.AddModule(10, app => app.Isolate(map => map.UseMvc(routes => {
builder.AddModule("MVC", 10, app => app.Isolate(map => map.UseMvc(routes => {
// Register the actions corresponding to the authorization endpoint.
if (builder.Options.AuthorizationEndpointPath.HasValue) {
routes.MapRoute("{D97891B4}", builder.Options.AuthorizationEndpointPath.Value.Substring(1), new {
@ -83,7 +84,7 @@ namespace Microsoft.AspNet.Builder {
options.FileProvider,
new EmbeddedFileProvider(
assembly: typeof(OpenIddictController<,>).GetTypeInfo().Assembly,
baseNamespace: "OpenIddict.Mvc"));
baseNamespace: typeof(OpenIddictController<,>).Namespace));
});
// Register the sign-in manager in the isolated container.

Loading…
Cancel
Save