Files
Backend-Api/ServiceHost/MiddleWare/CheckTaskMiddleware.cs

58 lines
2.0 KiB
C#

using System.Security.Claims;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;
using AccountManagement.Domain.TaskAgg;
namespace ServiceHost.MiddleWare
{
// You may need to install the Microsoft.AspNetCore.Http.Abstractions package into your project
public class CheckTaskMiddleware
{
private readonly RequestDelegate _next;
public CheckTaskMiddleware(RequestDelegate next)
{
_next = next;
}
public Task Invoke(HttpContext httpContext, ITaskRepository taskRepository)
{
if (httpContext.User.Identity is { IsAuthenticated: true })
{
if (httpContext.User.HasClaim(x => x is { Type: "AdminAreaPermission", Value: "true" }))
{
string path = httpContext.Request.Path.ToString();
if (!httpContext.User.HasClaim(x => (x is { Type: "RoleId", Value: "1" })) &&
!httpContext.Request.Path.Equals("/task", StringComparison.OrdinalIgnoreCase)&&
!httpContext.Request.Path.Equals("/serviceWorker", StringComparison.OrdinalIgnoreCase))
{
var result = taskRepository.HasOverdueTasks();
if (result)
{
httpContext.Response.Redirect("/task");
}
}
else
{
Console.WriteLine("else");
}
}
}
return _next(httpContext);
}
}
// Extension method used to add the middleware to the HTTP request pipeline.
public static class CheckTaskMiddlewareExtensions
{
public static IApplicationBuilder UseCheckTaskMiddleware(this IApplicationBuilder builder)
{
return builder.UseMiddleware<CheckTaskMiddleware>();
}
}
}