-4

I have a web applicaiton written in ASP.NET MVC 5. There is to be a landing page in .cshtml. When the user clicks on a button on this page, I am to check whether the user has already logged in (by checking the presence of a specific cookie and its valid value). If the user is already logged in, the landing page should be replaced by the target page. If the user is not logged in yet, a popup window is to be displayed for him to enter his login credentials. If he enters correctly, the login popup window should close, and then the landing page is to be replaced by the target page.

As I don't have prior experience with MVC, I need some help on this. When the user clicks on the button on the landing page, how to send the request to the server to check whether he is already logged in, and only replacing the landing page with the target page if the user is already logged in? Also, if the login popup window is showing, and the user enters a correct credentials, how to replace the landing page with target page?

A simple example would be good.

Thanks in advance.

user3573403
  • 1,780
  • 5
  • 38
  • 64
  • Possible duplicate of [Adding basic authentication to ASP MVC action](https://stackoverflow.com/questions/3680991/adding-basic-authentication-to-asp-mvc-action) – Arjun Prakash Dec 27 '17 at 06:37

1 Answers1

0

I am going to attempt to answer this from a higher level instead of provide a generic yet more technical example that is likely going to have to be re-coded to fit your needs.

You will need a boolean variable on your model, lets call it loggedIn. This will represent the logged in state of the user.

  • You want the controller to check the cookies first

  • If the cookie reads that they are logged in then assign loggedIn to true

  • If the cookie reads that they are not logged in then assign loggedIn to false

Once you pass your model to your view, you can then use RAZOR to read the loggedIn boolean. If it is true show the content. If it is false show the partial view for your log in.

  • Is the controller that you mentioned meant for the landing page (called before the landing page is displayed)? – user3573403 Dec 27 '17 at 02:24
  • Yes, for each page that required authentication via the log in you will need to set up a controller action. This means if you have 5 pages, you set up 5 controller actions. You can encapsulate the logic that reads the cookie and returns a boolean so that you just call it inside the controller action. – Brennien Coker Dec 27 '17 at 03:29
  • Your solution is just too vague and doesn't answer my questions. I do know what the controllers are and what they should do in my case. What I don't know how to do is how to trigger a controller when the button on the landing page is clicked. I'm thinking that perhaps an AJAX call is necessary here. – user3573403 Dec 27 '17 at 04:11
  • A vague answer to a vague question. MVC has what you need, but this could be done client side too. Perhaps provide more detail to your question. – Brennien Coker Dec 27 '17 at 04:15
  • Try looking at this answer: https://stackoverflow.com/questions/1606991/asp-net-mvc-http-authentication-prompt – Brennien Coker Dec 27 '17 at 14:45