Skip to content

Commit 4b55fb1

Browse files
authored
Add Identity server 4 (simplcommerce#831)
* Add Identity Server 4
1 parent a484912 commit 4b55fb1

11 files changed

Lines changed: 164 additions & 221 deletions

File tree

src/Modules/SimplCommerce.Module.Core/Areas/Core/Controllers/TokenApiController.cs

Lines changed: 0 additions & 109 deletions
This file was deleted.

src/Modules/SimplCommerce.Module.Core/ModuleInitializer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public void ConfigureServices(IServiceCollection serviceCollection)
1717
serviceCollection.AddTransient<IEntityService, EntityService>();
1818
serviceCollection.AddTransient<IMediaService, MediaService>();
1919
serviceCollection.AddTransient<IThemeService, ThemeService>();
20-
serviceCollection.AddTransient<ITokenService, TokenService>();
2120
serviceCollection.AddTransient<IWidgetInstanceService, WidgetInstanceService>();
2221
serviceCollection.AddScoped<SignInManager<User>, SimplSignInManager<User>>();
2322
serviceCollection.AddScoped<IWorkContext, WorkContext>();

src/Modules/SimplCommerce.Module.Core/Services/ITokenService.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/Modules/SimplCommerce.Module.Core/Services/TokenService.cs

Lines changed: 0 additions & 68 deletions
This file was deleted.

src/SimplCommerce.WebHost/Extensions/ApplicationBuilderExtensions.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using Microsoft.Extensions.Configuration;
1111
using Microsoft.Extensions.Hosting;
1212
using Microsoft.Net.Http.Headers;
13-
using SimplCommerce.Module.Core.Extensions;
1413
using SimplCommerce.Infrastructure.Data;
1514
using SimplCommerce.Infrastructure;
1615
using SimplCommerce.Infrastructure.Localization;
@@ -22,31 +21,33 @@ public static class ApplicationBuilderExtensions
2221
{
2322
public static IApplicationBuilder UseCustomizedIdentity(this IApplicationBuilder app)
2423
{
25-
app.UseAuthentication();
26-
app.UseAuthorization();
27-
24+
app.UseIdentityServer();
2825
app.UseWhen(
2926
context => context.Request.Path.StartsWithSegments("/api"),
3027
a => a.Use(async (context, next) =>
3128
{
32-
var principal = new ClaimsPrincipal();
29+
if (!context.User.Identity.IsAuthenticated) {
30+
var principal = new ClaimsPrincipal();
3331

34-
var cookiesAuthResult = await context.AuthenticateAsync("Identity.Application");
35-
if (cookiesAuthResult?.Principal != null)
36-
{
37-
principal.AddIdentities(cookiesAuthResult.Principal.Identities);
38-
}
32+
var cookiesAuthResult = await context.AuthenticateAsync("Identity.Application");
33+
if (cookiesAuthResult?.Principal != null)
34+
{
35+
principal.AddIdentities(cookiesAuthResult.Principal.Identities);
36+
}
3937

40-
var bearerAuthResult = await context.AuthenticateAsync(JwtBearerDefaults.AuthenticationScheme);
41-
if (bearerAuthResult?.Principal != null)
42-
{
43-
principal.AddIdentities(bearerAuthResult.Principal.Identities);
38+
var bearerAuthResult = await context.AuthenticateAsync(JwtBearerDefaults.AuthenticationScheme);
39+
if (bearerAuthResult?.Principal != null)
40+
{
41+
principal.AddIdentities(bearerAuthResult.Principal.Identities);
42+
}
43+
44+
context.User = principal;
4445
}
4546

46-
context.User = principal;
4747
await next();
4848
}));
4949

50+
app.UseAuthorization();
5051
return app;
5152
}
5253

src/SimplCommerce.WebHost/Extensions/ServiceCollectionExtensions.cs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,27 @@
66
using System.Net;
77
using System.Reflection;
88
using System.Runtime.Loader;
9-
using System.Text;
9+
using System.Security.Claims;
1010
using System.Threading.Tasks;
1111
using Microsoft.AspNetCore.Authentication;
1212
using Microsoft.AspNetCore.Authentication.Cookies;
1313
using Microsoft.AspNetCore.Authentication.JwtBearer;
1414
using Microsoft.AspNetCore.Authentication.OAuth;
1515
using Microsoft.AspNetCore.Identity;
1616
using Microsoft.AspNetCore.Http;
17+
using Microsoft.AspNetCore.Mvc.ApplicationParts;
18+
using Microsoft.Extensions.Localization;
1719
using Microsoft.EntityFrameworkCore;
1820
using Microsoft.Extensions.Configuration;
1921
using Microsoft.Extensions.DependencyInjection;
20-
using Microsoft.IdentityModel.Tokens;
2122
using Newtonsoft.Json;
2223
using SimplCommerce.Infrastructure;
2324
using SimplCommerce.Infrastructure.Modules;
2425
using SimplCommerce.Infrastructure.Web.ModelBinders;
2526
using SimplCommerce.Module.Core.Data;
2627
using SimplCommerce.Module.Core.Extensions;
2728
using SimplCommerce.Module.Core.Models;
28-
using Microsoft.AspNetCore.Mvc.ApplicationParts;
29-
using Microsoft.Extensions.Localization;
29+
using SimplCommerce.WebHost.IdentityServer;
3030

3131
namespace SimplCommerce.WebHost.Extensions
3232
{
@@ -164,6 +164,28 @@ public static IServiceCollection AddCustomizedIdentity(this IServiceCollection s
164164
.AddUserStore<SimplUserStore>()
165165
.AddDefaultTokenProviders();
166166

167+
services.AddIdentityServer(options =>
168+
{
169+
options.Events.RaiseErrorEvents = true;
170+
options.Events.RaiseInformationEvents = true;
171+
options.Events.RaiseFailureEvents = true;
172+
options.Events.RaiseSuccessEvents = true;
173+
})
174+
.AddInMemoryIdentityResources(IdentityServerConfig.Ids)
175+
.AddInMemoryApiResources(IdentityServerConfig.Apis)
176+
.AddInMemoryClients(IdentityServerConfig.Clients)
177+
.AddAspNetIdentity<User>()
178+
.AddProfileService<SimplProfileService>()
179+
.AddDeveloperSigningCredential(); // not recommended for production - you need to store your key material somewhere secure
180+
181+
// AddAspNetIdentity has change the option - switch back to default
182+
services.Configure<IdentityOptions>(options =>
183+
{
184+
options.ClaimsIdentity.UserIdClaimType = ClaimTypes.NameIdentifier;
185+
options.ClaimsIdentity.UserNameClaimType = ClaimTypes.Name;
186+
options.ClaimsIdentity.RoleClaimType = ClaimTypes.Role;
187+
});
188+
167189
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
168190
.AddCookie()
169191
.AddFacebook(x =>
@@ -187,16 +209,11 @@ public static IServiceCollection AddCustomizedIdentity(this IServiceCollection s
187209
})
188210
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
189211
{
190-
options.TokenValidationParameters = new TokenValidationParameters
191-
{
192-
ValidateIssuer = true,
193-
ValidateAudience = false,
194-
ValidateLifetime = true,
195-
ValidateIssuerSigningKey = true,
196-
ValidIssuer = configuration["Authentication:Jwt:Issuer"],
197-
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["Authentication:Jwt:Key"]))
198-
};
212+
options.Authority = "https://localhost:44388";
213+
options.RequireHttpsMetadata = false;
214+
options.Audience = "api.simplcommerce";
199215
});
216+
200217
services.ConfigureApplicationCookie(x =>
201218
{
202219
x.LoginPath = new PathString("/login");
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.Collections.Generic;
2+
using IdentityServer4.Models;
3+
4+
namespace SimplCommerce.WebHost.IdentityServer
5+
{
6+
public static class IdentityServerConfig
7+
{
8+
public static IEnumerable<IdentityResource> Ids =>
9+
new IdentityResource[]
10+
{
11+
new IdentityResources.OpenId(),
12+
new IdentityResources.Profile()
13+
};
14+
15+
public static IEnumerable<ApiResource> Apis =>
16+
new ApiResource[]
17+
{
18+
new ApiResource("api.simplcommerce", "SimplCommerce API")
19+
};
20+
21+
public static IEnumerable<Client> Clients =>
22+
new Client[]
23+
{
24+
new Client
25+
{
26+
ClientId = "ro.client",
27+
ClientName = "Resource Owner Client",
28+
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
29+
ClientSecrets = { new Secret("secret".Sha256()) },
30+
AllowedScopes = { "api.simplcommerce" }
31+
}
32+
};
33+
}
34+
}

0 commit comments

Comments
 (0)