Skip to content

Commit 046d1f0

Browse files
committed
Use AntiForgeryToken in swagger.
Resolve #5004
1 parent 66b821c commit 046d1f0

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Abp.Extensions;
55
using Abp.Notifications;
66
using Abp.Timing;
7+
using Abp.Web.Security.AntiForgery;
78
using AbpCompanyName.AbpProjectName.Controllers;
89

910
namespace AbpCompanyName.AbpProjectName.Web.Host.Controllers
@@ -12,13 +13,17 @@ public class HomeController : AbpProjectNameControllerBase
1213
{
1314
private readonly INotificationPublisher _notificationPublisher;
1415

15-
public HomeController(INotificationPublisher notificationPublisher)
16+
private readonly IAbpAntiForgeryManager _abpAntiForgeryManager;
17+
18+
public HomeController(INotificationPublisher notificationPublisher, IAbpAntiForgeryManager abpAntiForgeryManager)
1619
{
1720
_notificationPublisher = notificationPublisher;
21+
_abpAntiForgeryManager = abpAntiForgeryManager;
1822
}
1923

2024
public IActionResult Index()
2125
{
26+
_abpAntiForgeryManager.SetCookie(HttpContext);
2227
return Redirect("/swagger");
2328
}
2429

aspnet-core/src/AbpCompanyName.AbpProjectName.Web.Host/wwwroot/swagger/ui/abp.swagger.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ var abp = abp || {};
2626
return true;
2727
}
2828

29+
function addAntiForgeryTokenToXhr(xhr) {
30+
var antiForgeryToken = abp.security.antiForgery.getToken();
31+
if (antiForgeryToken) {
32+
xhr.setRequestHeader(abp.security.antiForgery.tokenHeaderName, antiForgeryToken);
33+
}
34+
}
35+
2936
function loginUserInternal(tenantId, callback) {
3037
var usernameOrEmailAddress = document.getElementById('userName').value;
3138
if (!usernameOrEmailAddress) {
@@ -58,6 +65,7 @@ var abp = abp || {};
5865
xhr.open('POST', '/api/TokenAuth/Authenticate', true);
5966
xhr.setRequestHeader('Abp.TenantId', tenantId);
6067
xhr.setRequestHeader('Content-type', 'application/json');
68+
addAntiForgeryTokenToXhr(xhr);
6169
xhr.send("{" + "usernameOrEmailAddress:'" + usernameOrEmailAddress + "'," + "password:'" + password + "'}");
6270
};
6371

@@ -81,6 +89,7 @@ var abp = abp || {};
8189

8290
xhrTenancyName.open('POST', '/api/services/app/Account/IsTenantAvailable', true);
8391
xhrTenancyName.setRequestHeader('Content-type', 'application/json');
92+
addAntiForgeryTokenToXhr(xhrTenancyName);
8493
xhrTenancyName.send("{" + "tenancyName:'" + tenancyName + "'}");
8594
} else {
8695
loginUserInternal(null, callback); // Login for host

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@
8181
configObject.requestInterceptor = function (request) {
8282
var token = abp.auth.getToken();
8383
request.headers.Authorization = token ? "Bearer " + token : null;
84+
var antiForgeryToken = abp.security.antiForgery.getToken();
85+
if (antiForgeryToken) {
86+
request.headers[abp.security.antiForgery.tokenHeaderName] = antiForgeryToken;
87+
}
8488
return request;
8589
};
8690
if (!configObject.hasOwnProperty("oauth2RedirectUrl")) {

0 commit comments

Comments
 (0)