-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom 401 http delegation handler
- Loading branch information
Showing
4 changed files
with
51 additions
and
13 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
backend/src/Designer/TypedHttpClients/DelegatingHandlers/Custom401Handler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Altinn.Studio.Designer.TypedHttpClients.DelegatingHandlers; | ||
|
||
public class Custom401Handler : DelegatingHandler | ||
{ | ||
private readonly IHttpContextAccessor _httpContextAccessor; | ||
|
||
public Custom401Handler(IHttpContextAccessor httpContextAccessor, HttpClientHandler innerHandler) : base(innerHandler) | ||
{ | ||
_httpContextAccessor = httpContextAccessor; | ||
} | ||
|
||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
var response = await base.SendAsync(request, cancellationToken); | ||
|
||
if (response.StatusCode == HttpStatusCode.Unauthorized) | ||
{ | ||
foreach (var cookie in _httpContextAccessor.HttpContext.Request.Cookies.Keys) | ||
{ | ||
_httpContextAccessor.HttpContext.Response.Cookies.Delete(cookie); | ||
_httpContextAccessor.HttpContext.Response.StatusCode = 401; | ||
} | ||
} | ||
|
||
return response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters