Skip to content

Commit 02b4dc4

Browse files
authored
Merge pull request #8 from ChaosEngine/dev
Configurable routes of IdentityManager2 simplyfying use with existing MVC app
2 parents deabfb8 + 11f5f9b commit 02b4dc4

6 files changed

Lines changed: 632 additions & 721 deletions

File tree

File renamed without changes.

src/IdentityManager2/Configuration/DependencyInjection/IdentityManagerServiceCollectionExtensions.cs

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using System;
2+
using System.Linq;
23
using IdentityManager2;
4+
using IdentityManager2.Api.Controllers;
35
using IdentityManager2.Configuration;
46
using Microsoft.AspNetCore.Http;
57
using Microsoft.AspNetCore.Mvc;
8+
using Microsoft.AspNetCore.Mvc.ApplicationModels;
69
using Microsoft.AspNetCore.Mvc.Infrastructure;
710
using Microsoft.AspNetCore.Mvc.Routing;
811
using 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
}

src/IdentityManager2/Configuration/SecurityConfiguration.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ public class SecurityConfiguration
1010
{
1111
public string HostAuthenticationType { get; set; }
1212
public string HostChallengeType { get; set; }
13+
public string AuthenticationScheme { get; set; }
1314
public string AdditionalSignOutType { get; set; }
15+
public string PageRouteAttribute { get; set; }
1416

1517
public string NameClaimType { get; set; }
1618
public string RoleClaimType { get; set; }
@@ -23,6 +25,8 @@ public SecurityConfiguration()
2325
NameClaimType = IdentityManagerConstants.ClaimTypes.Name;
2426
RoleClaimType = IdentityManagerConstants.ClaimTypes.Role;
2527
AdminRoleName = IdentityManagerConstants.AdminRoleName;
28+
AuthenticationScheme = IdentityManagerConstants.LocalApiScheme;
29+
PageRouteAttribute = null;
2630

2731
ShowLoginButton = true;
2832
}

src/IdentityManager2/IdentityManager2.csproj

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@
5050
</ItemGroup>
5151

5252
<ItemGroup>
53-
<PackageReference Include="AutoMapper" Version="7.0.1" />
54-
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
53+
<PackageReference Include="AutoMapper" Version="9.0.0" />
54+
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
5555
<PackageReference Include="System.ComponentModel.Annotations" Version="4.5.0" />
56-
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.0" />
57-
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.1.0" />
58-
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="2.1.0" />
59-
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.1.0" />
60-
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="2.1.0" />
56+
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
57+
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.2.0" />
58+
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="2.2.0" />
59+
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0" />
60+
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="2.2.0" />
6161
</ItemGroup>
6262

6363
<ItemGroup>

0 commit comments

Comments
 (0)