Keep redirecting the user until he completes a specific task

Asked

Viewed 42 times

0

I’m developing an app to manage a condo.

What I need is that after the user logs in he is redirected to the condominium registration screen (caso ele não tenha nenhum condomínio cadastrado ainda) regardless of which page he tries to access.

I tried to use Middleware, but it didn’t work for me.

I’ve been assigned to use Filters but I haven’t been able to implement.

They could help me ?

public class RedirectNoCondominium
{
    private readonly RequestDelegate _next;
    private readonly IHttpContextAccessor _httpContextAccessor;
    private readonly string path = "~/Condominium/Add";

    public RedirectNoCondominium(
        RequestDelegate next,
        IHttpContextAccessor httpContextAccessor)
    {
        _next = next;
        _httpContextAccessor = httpContextAccessor;
    }

    [Authorize]
    public async Task Invoke(HttpContext httpContext, ICondominiumService _condominiumManager)
    {
        if (httpContext.User.Identity.IsAuthenticated)
        {
            List<ApplicationCondominium> result = await _condominiumManager.GetCondominiumAsync();

            if (result.Count() == 0 && httpContext.Request.Path != path)
            {
                httpContext.Response.Redirect(path);
            }
            else
            {
                await _next(httpContext);
            }

        }
        else
        {
            await _next(httpContext);
        }
    }
}
  • And where are you implementing this "filter"?

  • I am trying to use it in all controllers, therefore I am using by dependency injection in the file startup,

  • 1

    Possible duplicate of Error while using Middleware

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.