From 91d70c3e172b8ecfcdba488406ed989473d14428 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Chalet?= Date: Thu, 12 Nov 2015 00:03:49 +0100 Subject: [PATCH] Add a new MVC convention to ensure that OpenIddictController gets an appropriate controller name --- src/OpenIddict.Mvc/OpenIddictExtensions.cs | 30 ++++++++++++++++------ 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/OpenIddict.Mvc/OpenIddictExtensions.cs b/src/OpenIddict.Mvc/OpenIddictExtensions.cs index 7af6c472..f220a6ba 100644 --- a/src/OpenIddict.Mvc/OpenIddictExtensions.cs +++ b/src/OpenIddict.Mvc/OpenIddictExtensions.cs @@ -11,6 +11,7 @@ using System.Reflection; using Microsoft.AspNet.FileProviders; using Microsoft.AspNet.Http; using Microsoft.AspNet.Identity; +using Microsoft.AspNet.Mvc.ApplicationModels; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Internal; using Microsoft.Extensions.Primitives; @@ -51,26 +52,22 @@ namespace Microsoft.AspNet.Builder { // Register the actions corresponding to the authorization endpoint. if (builder.Options.AuthorizationEndpointPath.HasValue) { routes.MapRoute("{D97891B4}", builder.Options.AuthorizationEndpointPath.Value.Substring(1), new { - controller = typeof(OpenIddictController<,>).Name, - action = nameof(OpenIddictController.Authorize) + controller = "OpenIddict", action = nameof(OpenIddictController.Authorize) }); routes.MapRoute("{7148DB83}", builder.Options.AuthorizationEndpointPath.Value.Substring(1) + "/accept", new { - controller = typeof(OpenIddictController<,>).Name, - action = nameof(OpenIddictController.Accept) + controller = "OpenIddict", action = nameof(OpenIddictController.Accept) }); routes.MapRoute("{23438BCC}", builder.Options.AuthorizationEndpointPath.Value.Substring(1) + "/deny", new { - controller = typeof(OpenIddictController<,>).Name, - action = nameof(OpenIddictController.Deny) + controller = "OpenIddict", action = nameof(OpenIddictController.Deny) }); } // Register the action corresponding to the logout endpoint. if (builder.Options.LogoutEndpointPath.HasValue) { routes.MapRoute("{C7DB102A}", builder.Options.LogoutEndpointPath.Value.Substring(1), new { - controller = typeof(OpenIddictController<,>).Name, - action = nameof(OpenIddictController.Logout) + controller = "OpenIddict", action = nameof(OpenIddictController.Logout) }); } }), services => { @@ -82,6 +79,10 @@ namespace Microsoft.AspNet.Builder { typeof(OpenIddictController<,>).MakeGenericType(registration.UserType, registration.ApplicationType) }) + // Add an OpenIddict-specific convention to ensure that the generic + // OpenIddictController gets an appropriate controller name. + .AddMvcOptions(options => options.Conventions.Add(new OpenIddictConvention())) + .AddRazorOptions(options => { // Update the Razor options to also use a combined provider that // falls back to the current assembly when searching for views. @@ -120,6 +121,19 @@ namespace Microsoft.AspNet.Builder { return builder; } + private class OpenIddictConvention : IControllerModelConvention { + public void Apply(ControllerModel controller) { + // Ensure the convention is only applied to the intended controller. + Debug.Assert(controller.ControllerType != null); + Debug.Assert(controller.ControllerType.IsGenericType); + Debug.Assert(controller.ControllerType.GetGenericTypeDefinition() == typeof(OpenIddictController<,>)); + + // Note: manually updating the controller name is required + // to remove the ending markers added to the generic type name. + controller.ControllerName = "OpenIddict"; + } + } + private class CombinedFileSystemProvider : IFileProvider { public CombinedFileSystemProvider(IList providers) { Providers = providers;