Skip to content

Commit ea01ef0

Browse files
authored
Merge pull request #490 from aspnetboilerplate/maliming/patch-1
Fixed the issue that anti forgery failed after user identity changed.
2 parents f3151bd + 9e2f744 commit ea01ef0

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
1+
using System.Threading.Tasks;
2+
using Abp.Web.Security.AntiForgery;
13
using Microsoft.AspNetCore.Antiforgery;
24
using AbpCompanyName.AbpProjectName.Controllers;
5+
using Microsoft.AspNetCore.Mvc;
36

47
namespace AbpCompanyName.AbpProjectName.Web.Host.Controllers
58
{
69
public class AntiForgeryController : AbpProjectNameControllerBase
710
{
811
private readonly IAntiforgery _antiforgery;
12+
private readonly IAbpAntiForgeryManager _antiForgeryManager;
913

10-
public AntiForgeryController(IAntiforgery antiforgery)
14+
public AntiForgeryController(IAntiforgery antiforgery, IAbpAntiForgeryManager antiForgeryManager)
1115
{
1216
_antiforgery = antiforgery;
17+
_antiForgeryManager = antiForgeryManager;
1318
}
1419

1520
public void GetToken()
1621
{
1722
_antiforgery.SetCookieTokenAndHeader(HttpContext);
1823
}
24+
25+
public void SetCookie()
26+
{
27+
_antiForgeryManager.SetCookie(HttpContext);
28+
}
1929
}
2030
}

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: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
119119
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
120120
endpoints.MapControllerRoute("defaultWithArea", "{area}/{controller=Home}/{action=Index}/{id?}");
121121
});
122-
123-
122+
124123
// Enable middleware to serve generated Swagger as a JSON endpoint
125124
app.UseSwagger();
126125
// 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: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,21 @@
7070
<script src="swagger-ui-standalone-preset.js"></script>
7171
<script src="ui/abp.js"></script>
7272
<script src="ui/abp.swagger.js"></script>
73+
7374
<script>
7475
window.onload = function () {
76+
var authToken = abp.auth.getToken();
77+
if (authToken) {
78+
fetch("/AntiForgery/SetCookie",
79+
{
80+
headers: {
81+
"Authorization": 'Bearer ' + authToken
82+
}
83+
});
84+
} else {
85+
fetch("/AntiForgery/SetCookie");
86+
}
87+
7588
var configObject = JSON.parse('%(ConfigObject)');
7689

7790
// Apply mandatory parameters
@@ -109,16 +122,12 @@
109122
lineHeight: "normal"
110123
},
111124
onClick: function () {
112-
var authorizeButton = document.getElementById('authorize');
113125
if (abp.auth.getToken()) {
114126
abp.swagger.logout();
115-
authorizeButton.innerText = getAuthorizeButtonText();
116-
authorizeButton.className = 'btn ' + getAuthorizeButtonCssClass();
127+
location.reload();
117128
} else {
118129
abp.swagger.openAuthDialog(function () {
119-
authorizeButton.innerText = getAuthorizeButtonText();
120-
authorizeButton.className = 'btn ' + getAuthorizeButtonCssClass();
121-
abp.swagger.closeAuthDialog();
130+
location.reload();
122131
});
123132
}
124133
}

0 commit comments

Comments
 (0)