11using System ;
2+ using System . Linq ;
23using IdentityManager2 ;
4+ using IdentityManager2 . Api . Controllers ;
35using IdentityManager2 . Configuration ;
46using Microsoft . AspNetCore . Http ;
57using Microsoft . AspNetCore . Mvc ;
8+ using Microsoft . AspNetCore . Mvc . ApplicationModels ;
69using Microsoft . AspNetCore . Mvc . Infrastructure ;
710using Microsoft . AspNetCore . Mvc . Routing ;
811using Microsoft . Extensions . DependencyInjection . Extensions ;
@@ -25,7 +28,15 @@ public static IIdentityManagerBuilder AddIdentityManager(this IServiceCollection
2528
2629 var builder = services . AddIdentityManagerBuilder ( ) ;
2730
28- builder . Services . AddMvc ( ) ;
31+ if ( string . IsNullOrEmpty ( identityManagerOptions . SecurityConfiguration . PageRouteAttribute ) )
32+ builder . Services . AddMvc ( ) ;
33+ else
34+ {
35+ builder . Services . AddMvc ( opt =>
36+ {
37+ opt . UseCentralRoutePrefix ( new RouteAttribute ( identityManagerOptions . SecurityConfiguration . PageRouteAttribute ) ) ;
38+ } ) ;
39+ }
2940 builder . Services . AddOptions ( ) ;
3041 builder . Services . AddSingleton ( identityManagerOptions ) ;
3142
@@ -45,20 +56,25 @@ public static IIdentityManagerBuilder AddIdentityManager(this IServiceCollection
4556 options . AddPolicy ( IdentityManagerConstants . IdMgrAuthPolicy , config =>
4657 {
4758 config . RequireClaim ( identityManagerOptions . SecurityConfiguration . RoleClaimType , identityManagerOptions . SecurityConfiguration . AdminRoleName ) ;
48- config . AddAuthenticationSchemes ( IdentityManagerConstants . LocalApiScheme ) ;
59+
60+ if ( ! string . IsNullOrEmpty ( identityManagerOptions . SecurityConfiguration . AuthenticationScheme ) )
61+ config . AddAuthenticationSchemes ( identityManagerOptions . SecurityConfiguration . AuthenticationScheme ) ;
4962 } ) ;
5063 } ) ;
5164
52- services . AddAuthentication ( )
53- . AddCookie ( IdentityManagerConstants . LocalApiScheme , options =>
54- {
55- options . Cookie . SameSite = SameSiteMode . Strict ;
56- options . Cookie . HttpOnly = true ;
57- options . Cookie . IsEssential = true ;
58- options . Cookie . SecurePolicy = CookieSecurePolicy . SameAsRequest ;
65+ if ( ! string . IsNullOrEmpty ( identityManagerOptions . SecurityConfiguration . AuthenticationScheme ) )
66+ {
67+ services . AddAuthentication ( )
68+ . AddCookie ( identityManagerOptions . SecurityConfiguration . AuthenticationScheme , options =>
69+ {
70+ options . Cookie . SameSite = SameSiteMode . Strict ;
71+ options . Cookie . HttpOnly = true ;
72+ options . Cookie . IsEssential = true ;
73+ options . Cookie . SecurePolicy = CookieSecurePolicy . SameAsRequest ;
5974
60- options . LoginPath = "/api/login" ;
61- } ) ;
75+ options . LoginPath = "/api/login" ;
76+ } ) ;
77+ }
6278
6379 identityManagerOptions . SecurityConfiguration . Configure ( services ) ;
6480
@@ -77,4 +93,36 @@ public static IIdentityManagerBuilder AddIdentityManagerBuilder(this IServiceCol
7793 return new IdentityManagerBuilder ( services ) ;
7894 }
7995 }
96+
97+ class RouteConvention : IApplicationModelConvention
98+ {
99+ private readonly IRouteTemplateProvider _routeTemplateProvider ;
100+
101+ public RouteConvention ( IRouteTemplateProvider routeTemplateProvider )
102+ {
103+ _routeTemplateProvider = routeTemplateProvider ;
104+ }
105+
106+ public void Apply ( ApplicationModel application )
107+ {
108+ var matchedSelectors = application . Controllers . FirstOrDefault ( c => c . ControllerType == typeof ( PageController ) ) ? . Selectors ;
109+ if ( matchedSelectors != null && matchedSelectors . Any ( ) )
110+ {
111+ var centralPrefix = new AttributeRouteModel ( _routeTemplateProvider ) ;
112+ foreach ( var selectorModel in matchedSelectors )
113+ {
114+ selectorModel . AttributeRouteModel = AttributeRouteModel . CombineAttributeRouteModel ( centralPrefix ,
115+ selectorModel . AttributeRouteModel ) ;
116+ }
117+ }
118+ }
119+ }
120+
121+ static class MvcOptionsExtensions
122+ {
123+ public static void UseCentralRoutePrefix ( this MvcOptions opts , IRouteTemplateProvider routeAttribute )
124+ {
125+ opts . Conventions . Insert ( 0 , new RouteConvention ( routeAttribute ) ) ;
126+ }
127+ }
80128}
0 commit comments