Skip to content

Commit 2d1ca3a

Browse files
committed
Add middleware to enable token based authentication.
1 parent 5ca0c66 commit 2d1ca3a

File tree

6 files changed

+44
-6
lines changed

6 files changed

+44
-6
lines changed

aspnet-core/src/AbpCompanyName.AbpProjectName.Application/AbpCompanyName.AbpProjectName.Application.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
1212
<RootNamespace>AbpCompanyName.AbpProjectName</RootNamespace>
1313
</PropertyGroup>
14-
14+
1515
<ItemGroup>
1616
<ProjectReference Include="..\AbpCompanyName.AbpProjectName.Core\AbpCompanyName.AbpProjectName.Core.csproj" />
1717
</ItemGroup>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Microsoft.AspNetCore.Authentication;
2+
using Microsoft.AspNetCore.Authentication.JwtBearer;
3+
using Microsoft.AspNetCore.Builder;
4+
5+
namespace AbpCompanyName.AbpProjectName.Authentication.JwtBearer
6+
{
7+
public static class JwtTokenMiddleware
8+
{
9+
public static IApplicationBuilder UseJwtTokenMiddleware(this IApplicationBuilder app)
10+
{
11+
return app.Use(async (ctx, next) =>
12+
{
13+
if (ctx.User.Identity?.IsAuthenticated != true)
14+
{
15+
var result = await ctx.AuthenticateAsync(JwtBearerDefaults.AuthenticationScheme);
16+
if (result.Succeeded && result.Principal != null)
17+
{
18+
ctx.User = result.Principal;
19+
}
20+
}
21+
22+
await next();
23+
});
24+
}
25+
}
26+
}

aspnet-core/src/AbpCompanyName.AbpProjectName.Web.Host/Startup/AuthConfigurer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public static void Configure(IServiceCollection services, IConfiguration configu
1919
services.AddAuthentication()
2020
.AddJwtBearer(options =>
2121
{
22+
options.Audience = configuration["Authentication:JwtBearer:Audience"];
23+
2224
options.TokenValidationParameters = new TokenValidationParameters
2325
{
2426
// The signing key must match!

aspnet-core/src/AbpCompanyName.AbpProjectName.Web.Host/Startup/Startup.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
using Microsoft.Extensions.Logging;
1414
using Swashbuckle.AspNetCore.Swagger;
1515
using Abp.Extensions;
16+
using Microsoft.AspNetCore.Authentication;
17+
using Microsoft.AspNetCore.Authentication.JwtBearer;
18+
using AbpCompanyName.AbpProjectName.Authentication.JwtBearer;
1619

1720
#if FEATURE_SIGNALR
1821
using Owin;
@@ -83,10 +86,11 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
8386

8487
app.UseCors(DefaultCorsPolicyName); //Enable CORS!
8588

86-
app.UseAuthentication();
87-
8889
app.UseStaticFiles();
8990

91+
app.UseAuthentication();
92+
app.UseJwtTokenMiddleware();
93+
9094
#if FEATURE_SIGNALR
9195
//Integrate to OWIN
9296
app.UseAppBuilder(ConfigureOwinServices);

aspnet-core/src/AbpCompanyName.AbpProjectName.Web.Mvc/Startup/AuthConfigurer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Text;
3+
using Microsoft.AspNetCore.Authentication.JwtBearer;
34
using Microsoft.Extensions.Configuration;
45
using Microsoft.Extensions.DependencyInjection;
56
using Microsoft.IdentityModel.Tokens;
@@ -15,6 +16,8 @@ public static void Configure(IServiceCollection services, IConfiguration configu
1516
services.AddAuthentication()
1617
.AddJwtBearer(options =>
1718
{
19+
options.Audience = configuration["Authentication:JwtBearer:Audience"];
20+
1821
options.TokenValidationParameters = new TokenValidationParameters
1922
{
2023
// The signing key must match!

aspnet-core/src/AbpCompanyName.AbpProjectName.Web.Mvc/Startup/Startup.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using Abp.AspNetCore;
33
using Abp.Castle.Logging.Log4Net;
4+
using AbpCompanyName.AbpProjectName.Authentication.JwtBearer;
45
using AbpCompanyName.AbpProjectName.Configuration;
56
using AbpCompanyName.AbpProjectName.Identity;
67
using AbpCompanyName.AbpProjectName.Web.Resources;
@@ -11,6 +12,8 @@
1112
using Microsoft.Extensions.Configuration;
1213
using Microsoft.Extensions.DependencyInjection;
1314
using Microsoft.Extensions.Logging;
15+
using Microsoft.AspNetCore.Authentication;
16+
using Microsoft.AspNetCore.Authentication.JwtBearer;
1417

1518
#if FEATURE_SIGNALR
1619
using Owin;
@@ -38,7 +41,6 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
3841
});
3942

4043
IdentityRegistrar.Register(services);
41-
4244
AuthConfigurer.Configure(services, _appConfiguration);
4345

4446
services.AddScoped<IWebResourceManager, WebResourceManager>();
@@ -66,10 +68,11 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
6668
app.UseExceptionHandler("/Error");
6769
}
6870

69-
app.UseAuthentication();
70-
7171
app.UseStaticFiles();
7272

73+
app.UseAuthentication();
74+
app.UseJwtTokenMiddleware();
75+
7376
#if FEATURE_SIGNALR
7477
//Integrate to OWIN
7578
app.UseAppBuilder(ConfigureOwinServices);

0 commit comments

Comments
 (0)