Browse Source

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

pull/31/head
Kévin Chalet 10 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.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<dynamic, dynamic>.Authorize)
controller = "OpenIddict", action = nameof(OpenIddictController<object, object>.Authorize)
});
routes.MapRoute("{7148DB83}", builder.Options.AuthorizationEndpointPath.Value.Substring(1) + "/accept", new {
controller = typeof(OpenIddictController<,>).Name,
action = nameof(OpenIddictController<dynamic, dynamic>.Accept)
controller = "OpenIddict", action = nameof(OpenIddictController<object, object>.Accept)
});
routes.MapRoute("{23438BCC}", builder.Options.AuthorizationEndpointPath.Value.Substring(1) + "/deny", new {
controller = typeof(OpenIddictController<,>).Name,
action = nameof(OpenIddictController<dynamic, dynamic>.Deny)
controller = "OpenIddict", action = nameof(OpenIddictController<object, object>.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<dynamic, dynamic>.Logout)
controller = "OpenIddict", action = nameof(OpenIddictController<object, object>.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<IFileProvider> providers) {
Providers = providers;

Loading…
Cancel
Save