Check if any user has logged in, and pick up the logged in user - core 2.1

Asked

Viewed 119 times

1

Hello I need to check if there is any user authentication, and after checking if any user has authenticated to get this user’s ID.

I own a Middleware that redirects the user to condominium registration screen, not letting him do anything until you register a condominium.

The problem I have is that Middleware runs before a user even logs in.

I need to change this to run only after an authentication happens.

Middleware:

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;
    }

    public async Task Invoke(HttpContext httpContext, ICondominiumService _condominiumManager)
    {

        if (user != null)
        {

            List<ApplicationCondominium> result = await _condominiumManager.GetCondominiumAsync(user);

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

    }
}

and call her in my startup:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {

        app.UseMiddleware<RedirectNoCondominium>();

        app.UseForwardedHeaders();

        app.Use(async (context, next) =>
        {
            if (context.Request.IsHttps || context.Request.Headers["X-Forwarded-Proto"] == Uri.UriSchemeHttps)
            {
                await next();
            }
            else
            {
                string queryString = context.Request.QueryString.HasValue ? context.Request.QueryString.Value : string.Empty;
                var https = "https://" + context.Request.Host + context.Request.Path + queryString;
                context.Response.Redirect(https);
            }
        });


        if (env.IsDevelopment())
        {

            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }


        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();

        app.UseAuthentication();

        app.UseNToastNotify();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
  • I think the right thing would be to use Filters, his Pipe happens in the execution of the action. https://www.c-sharpcorner.com/article/working-with-filters-in-asp-net-core-mvc/ in it vc would also change the response to a Redirectactionresult.

  • I could set an example ?

  • 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.