Skip to content

Commit 3f73857

Browse files
committed
Fixed the issue that anti forgery failed after user identity changed.
1 parent f3151bd commit 3f73857

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

aspnet-core/src/AbpCompanyName.AbpProjectName.Web.Host/Controllers/HomeController.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,13 @@ public class HomeController : AbpProjectNameControllerBase
1313
{
1414
private readonly INotificationPublisher _notificationPublisher;
1515

16-
private readonly IAbpAntiForgeryManager _abpAntiForgeryManager;
17-
18-
public HomeController(INotificationPublisher notificationPublisher, IAbpAntiForgeryManager abpAntiForgeryManager)
16+
public HomeController(INotificationPublisher notificationPublisher)
1917
{
2018
_notificationPublisher = notificationPublisher;
21-
_abpAntiForgeryManager = abpAntiForgeryManager;
2219
}
2320

2421
public IActionResult Index()
2522
{
26-
_abpAntiForgeryManager.SetCookie(HttpContext);
2723
return Redirect("/swagger");
2824
}
2925

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Linq;
33
using System.Reflection;
4+
using System.Text.RegularExpressions;
45
using Microsoft.AspNetCore.Builder;
56
using Microsoft.AspNetCore.Hosting;
67
using Microsoft.Extensions.Configuration;
@@ -16,6 +17,7 @@
1617
using Abp.AspNetCore.SignalR.Hubs;
1718
using Abp.Dependency;
1819
using Abp.Json;
20+
using Abp.Web.Security.AntiForgery;
1921
using Microsoft.OpenApi.Models;
2022
using Newtonsoft.Json.Serialization;
2123

@@ -120,7 +122,20 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
120122
endpoints.MapControllerRoute("defaultWithArea", "{area}/{controller=Home}/{action=Index}/{id?}");
121123
});
122124

125+
// Swagger anti forgery
126+
app.Use(async (context, next) =>
127+
{
128+
const string swaggerRoutePrefix = "swagger";
123129

130+
if (context.Request.Method == "GET" &&
131+
context.Request.Path.HasValue &&
132+
context.Request.Path.Value.StartsWith(swaggerRoutePrefix.EnsureStartsWith('/')))
133+
{
134+
context.RequestServices.GetRequiredService<IAbpAntiForgeryManager>().SetCookie(context);
135+
}
136+
137+
await next.Invoke();
138+
});
124139
// Enable middleware to serve generated Swagger as a JSON endpoint
125140
app.UseSwagger();
126141
// Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)

aspnet-core/src/AbpCompanyName.AbpProjectName.Web.Host/wwwroot/swagger/ui/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
abp.swagger.closeAuthDialog();
122122
});
123123
}
124+
location.reload();
124125
}
125126
}, getAuthorizeButtonText());
126127
}

0 commit comments

Comments
 (0)