0

so from website.com/Admin I need to display a form that gets the key from the user, after the key=="my password" it displays a button that redirects to the table view page, this page should only display something only if the key was equal to my password

ControllerAdminPage

public IActionResult Login()
        {
            AdminModel keyModel = new AdminModel();
            keyModel.key = Request.Form["key"];
            ViewData["AdminModel"] = keyModel;
            return View("Admin", keyModel);
        }

How can I access that specific keyModel.key from another controller ?

ControllerOfTablePage

public async Task<IActionResult> Index()
        {

            return View(await _context.Links.ToListAsync());
        }

I've tried to do something like this in .cshtml pages:

ReadTableView

@using WebPage.Models
@model IEnumerable<WebPage.Models.LinkModel>
@{
    ViewData["Title"] = "Index";
    var admin = ViewData["AdminModel"] as AdminModel;
}
@if (admin?.key == "pwd") //logic of Read Table

AdminPageView(WhereTheFormOfKeyIs)

@using WebPage.Models
@{
    ViewData["Title"] = "Admin";
    var admin = ViewData["AdminModel"] as AdminModel;
}
@if (admin?.key == "pwd")
{
    
        LOGGED IN SUCCESFULLY
        
        <form method="post" asp-action="ViewLinks">
            <input type="submit" value="Links Table"/>
        </form>
        
    </div>
}
else
{
    

        <div id="login_form">
            <form method="post" asp-action="Login">
                Key:
                <input type="text" name="key" />

                <input type="submit" value="Log in" />
            </form>
        </div>


    </body>
}
jujy7
  • 21
  • 2
  • Why not compare the keys in the `Login` and stop redirection if the key is not correct? – Chetan Nov 01 '22 at 04:15
  • Yes, but if I enter the site directly from the link of the table it suppose to not work since I didn’t enter any password. So how could I pass the key to other controller ? – jujy7 Nov 01 '22 at 08:25
  • I think what you are asking is how to maintain data between requests. HTTP requests are generally stateless. You will need to implement something that holds the state between requests to achieve what you want. Take a look at this https://stackoverflow.com/questions/10756140/asp-net-mvc-and-state-how-to-keep-state-between-requests. – Charles Han Nov 06 '22 at 07:49

0 Answers0