Browse Source

Add a new MVC convention to ensure that OpenIddictController gets an appropriate controller name

pull/31/head
Kévin Chalet 11 years ago
parent
commit
91d70c3e17
  1. 30
      src/OpenIddict.Mvc/OpenIddictExtensions.cs

30
src/OpenIddict.Mvc/OpenIddictExtensions.cs

@ -11,6 +11,7 @@ using System.Reflection;
using Microsoft.AspNet.FileProviders; using Microsoft.AspNet.FileProviders;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Mvc.ApplicationModels;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Internal; using Microsoft.Extensions.Internal;
using Microsoft.Extensions.Primitives; using Microsoft.Extensions.Primitives;
@ -51,26 +52,22 @@ namespace Microsoft.AspNet.Builder {
// Register the actions corresponding to the authorization endpoint. // Register the actions corresponding to the authorization endpoint.
if (builder.Options.AuthorizationEndpointPath.HasValue) { if (builder.Options.AuthorizationEndpointPath.HasValue) {
routes.MapRoute("{D97891B4}", builder.Options.AuthorizationEndpointPath.Value.Substring(1), new { routes.MapRoute("{D97891B4}", builder.Options.AuthorizationEndpointPath.Value.Substring(1), new {
controller = typeof(OpenIddictController<,>).Name, controller = "OpenIddict", action = nameof(OpenIddictController<object, object>.Authorize)
action = nameof(OpenIddictController<dynamic, dynamic>.Authorize)
}); });
routes.MapRoute("{7148DB83}", builder.Options.AuthorizationEndpointPath.Value.Substring(1) + "/accept", new { routes.MapRoute("{7148DB83}", builder.Options.AuthorizationEndpointPath.Value.Substring(1) + "/accept", new {
controller = typeof(OpenIddictController<,>).Name, controller = "OpenIddict", action = nameof(OpenIddictController<object, object>.Accept)
action = nameof(OpenIddictController<dynamic, dynamic>.Accept)
}); });
routes.MapRoute("{23438BCC}", builder.Options.AuthorizationEndpointPath.Value.Substring(1) + "/deny", new { routes.MapRoute("{23438BCC}", builder.Options.AuthorizationEndpointPath.Value.Substring(1) + "/deny", new {
controller = typeof(OpenIddictController<,>).Name, controller = "OpenIddict", action = nameof(OpenIddictController<object, object>.Deny)
action = nameof(OpenIddictController<dynamic, dynamic>.Deny)
}); });
} }
// Register the action corresponding to the logout endpoint. // Register the action corresponding to the logout endpoint.
if (builder.Options.LogoutEndpointPath.HasValue) { if (builder.Options.LogoutEndpointPath.HasValue) {
routes.MapRoute("{C7DB102A}", builder.Options.LogoutEndpointPath.Value.Substring(1), new { routes.MapRoute("{C7DB102A}", builder.Options.LogoutEndpointPath.Value.Substring(1), new {
controller = typeof(OpenIddictController<,>).Name, controller = "OpenIddict", action = nameof(OpenIddictController<object, object>.Logout)
action = nameof(OpenIddictController<dynamic, dynamic>.Logout)
}); });
} }
}), services => { }), services => {
@ -82,6 +79,10 @@ namespace Microsoft.AspNet.Builder {
typeof(OpenIddictController<,>).MakeGenericType(registration.UserType, registration.ApplicationType) 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 => { .AddRazorOptions(options => {
// Update the Razor options to also use a combined provider that // Update the Razor options to also use a combined provider that
// falls back to the current assembly when searching for views. // falls back to the current assembly when searching for views.
@ -120,6 +121,19 @@ namespace Microsoft.AspNet.Builder {
return 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 { private class CombinedFileSystemProvider : IFileProvider {
public CombinedFileSystemProvider(IList<IFileProvider> providers) { public CombinedFileSystemProvider(IList<IFileProvider> providers) {
Providers = providers; Providers = providers;

Loading…
Cancel
Save