locked
Which Controller method is being called “Only once” in ASP.NET MVC? RRS feed

  • Question

  • User2076229911 posted

    I am developing an application which including user register, login and some other stuff. I have "Remember Me" feature when user login. But I also have to log every visit of the user through a web service.

    I tried protected override void Initialize(System.Web.Routing.RequestContext requestContext) on home page but it is called every time the page is called.

    I am using cookies so global.asax application_start is not working. Is there anyone who did something like that before_ What is the right approach for this?

    Friday, May 15, 2020 8:45 AM

All replies

  • User-17257777 posted

    Hi didemyosunlu,

    But I also have to log every visit of the user through a web service.

    Why not log user visit in your login method? You can record user access information after he passes the authentication. Usually, one session only includes once user login.

    Best Regards,

    Jiadong Meng,

    Friday, May 15, 2020 9:43 AM
  • User2076229911 posted

    Hi jiadongm,

    The main thing is I have also a User management Portal and in this portal a user can be blocked. So for example a user logged in the application with checking remember me checkbox. Did some stuff for a while and close the browser. After two days he/she came back. But before that a Portal user blocked her/him. So when she/he open the application I check the user from cookies and is not null I put cookie to session and allow the user to continue as if he/she logged in. So I need to check when application open the first time and also log this as if he/she logged in. So I need somewhere to check cookies and go to database to log this visit "Only once" through this visit.

    I hope it is more clear now.

    Friday, May 15, 2020 10:49 AM
  • User753101303 posted

    Hi,

    didemyosunlu

    I put cookie to session

    So if you find a valid cookie but it is not in Session already it means the user returns and this is where you could log that? Do you have a problem with this particular point?

    Friday, May 15, 2020 11:29 AM
  • User2076229911 posted

    Yes and I have to do this just once when the user opens the application.

    Friday, May 15, 2020 11:31 AM
  • User753101303 posted

    Ok you don't have any written any code for this? For now my understanding is that you are rolling your own authentication/remember feature (you could consider to use what ASP.NET offers out of the box later). So I believe you should :

    • check if the browser "IsAuthenticated" session variable contains something, if yes fine
      - if not, check if you find a user cookie, validate that and copy/or set the IsAuthenticated variable exactly as you described and you can log here this is a returning user
      - else going to the login page

    Friday, May 15, 2020 11:45 AM
  • User2076229911 posted

    Yes but where to put this logging block. Because I will log this as a login. So I have to do this once.

    I cant put in Home controller Index method or others because they can be called multiple times. I cant put in Application_Start() because there is no Cookies in there yet. 

    So I need a place where hit only once when user opens the applicaiton.

    Friday, May 15, 2020 12:34 PM
  • User-474980206 posted

    You need to a browser cookie with a sliding expiration that you check after authentication. If it not there count as login, and set the cookie. If asp.net core use middle ware, if classic, the global.asax, the on AuthorizeRequest.

    if you have not extended session timeout, you can set a value in session. 

    Friday, May 15, 2020 2:26 PM
  • User2076229911 posted

    the global.asax, the on AuthorizeRequest.

    But then for each request I will be logging as user login. I need to log this just once when user opens the browser.

    Monday, May 18, 2020 6:20 AM
  • User753101303 posted

    It will run for each request. You'll just have an "if" statement that will check if your session variable is already set (in which case you can skip the whole block) or if you need to trigger this "remember me" logic...

    Monday, May 18, 2020 7:07 AM
  • User2076229911 posted

    I tried to implement this but for example I close the appliation and open after 15 minutes. session is still there so I dont understand if the application open the first time or I am just navigating between pages. Any other suggestion?

    Thursday, May 21, 2020 6:11 AM