How to quickly make a temporary separate login/password form to display a closed website on the Internet?

Volk Volk 551 Reputation points
2023-10-04T14:01:39.5833333+00:00

Hi!

The site (Net.Core 6) already has a working authentication, but it can only be used if temporary login is allowed.

This site needs to be temporarily hidden for everyone on the Internet.It is necessary to make a temporary entrance to the site through the login/password form (login/password is just the assigned text).

That is, I upload the site to the network at https, but all its pages should be hidden for everyone except those who have a certain username/password.

There are a lot of pages on the site, there is a separate authentication inside the site.

Is there a way to quickly implement such an input without prescribing conditions to all methods in the controller that return a Views?

Has anyone ever faced such a task?

Can you please advise a quick solution?

Thanks!

Developer technologies | ASP.NET | ASP.NET Core
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 78,006 Reputation points Volunteer Moderator
    2023-10-04T18:47:30.08+00:00

    your question is not clear, but I think you want to add a login facade to your app. the easiest to add your own middleware that checks for authentication,. trival example

    ...
    app.UseHttpsRedirection();
    app.Use(async (context, next) =>
    {
        var cookie = context.Request.Cookies["templogin"];
        if (cookie?.FirstOrDefault().ToString() != "ok")
        {
            if (context.Request.Query["user"].FirstOrDefault() == "test"
                && context.Request.Query["password"].FirstOrDefault() == "password")
            {
                context.Response.Cookies.Append("templogin", "ok", new CookieOptions
                {
                    Expires = DateTime.Now.AddDays(1),
                    Path = "/"
                });
            }
            else
            {
                await context.Response.WriteAsync("Site not available");
                return;
            }
        }
    
        // Call the next delegate/middleware in the pipeline.
        await next(context);
    });
    ...
    

    to login:

    https://mysite.com?user=test&password=password

    note: to improve security use an encrypted token for "ok". and a database (or environment variables) for user & password hash


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.