Repository navigation
Expand file tree
/
Copy pathCustomErrorHandler.cs
More file actions
54 lines (51 loc) · 2.09 KB
/
Copy pathCustomErrorHandler.cs
File metadata and controls
54 lines (51 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
namespace CohortTool
{
/// <summary>
/// Custom Error Handler redirect to user-friendly error page.
/// 1) Default Auth Server State Token life time is 15 minutes.
/// This custom error handler looks for error description from openiddict and
/// renew state token by redirecting user to home page.
/// 2) Any status code greater then 400 will be redirected to error page.
/// </summary>
public class CustomErrorHandler
{
private readonly RequestDelegate _next;
private readonly ILogger<CustomErrorHandler> _logger;
public CustomErrorHandler(RequestDelegate next, ILogger<CustomErrorHandler> logger)
{
_next = next;
_logger = logger;
}
public async Task Invoke(HttpContext context)
{
var originalBodyStream = context.Response.Body;
using (var responseBody = new MemoryStream())
{
context.Response.Body = responseBody;
await _next(context);
context.Response.Body.Seek(0, SeekOrigin.Begin);
int statusCode = context.Response.StatusCode;
if (statusCode == 400 && await RedirectOpenIdDictResponse(context, statusCode))
return;
if (statusCode >= 400)
{
context.Response.Clear();
context.Response.Redirect("/Home/Error?StatusCode=" + statusCode);
return;
}
await responseBody.CopyToAsync(originalBodyStream);
}
}
private static async Task<bool> RedirectOpenIdDictResponse(HttpContext context, int statusCode)
{
string responseContent = await new StreamReader(context.Response.Body).ReadToEndAsync();
if (responseContent?.Contains("Invalid_token") == true || responseContent?.Contains("openiddict") == true)
{
context.Response.Clear();
context.Response.Redirect("/");
return true;
}
return false;
}
}
}