Best way to redirect user to Login page on cache lost

Alexandre Hass

What is the best way to redirect user to login when cache is lost? At moment I'm doing this:

if (Session["Id"] == null)
{
    return RedirectToAction("Login", "Home");
}

But with this method, it put this code in every function, is there any other way to do this in the entire program? I tried search by doing this from web.config, but no results.

Aamol

Assuming you are using Form-based authentication and permitting access to only authorised users then this could be achieved through making changes in web.config

<authentication mode="Forms">
      <forms loginUrl="login.aspx" defaultUrl="default.aspx" name=".YourApplication" timeout="60" cookieless="AutoDetect" />
</authentication>

<authorization>
      <deny users="?" />
</authorization>

the loginUrl property tells the server where to direct the user if he is not logged in, and the defaultUrl property tells the server where to direct him after the user is logged.

the deny users="?" tells the server to deny any user that is not authenticated and directs him to the loginUrl page

In the codebehind of the login page, you need something like this after you check the credentials inserted:

FormsAuthentication.RedirectFromLoginPage(userName.Text, True)

Reference

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Redirect User back to Page on Login

Redirect to login page if user is not logged in

Next.js - Best way to redirect to another page if user gets to a specific url

After signup redirect user to login page

Django - Login and redirect to user profile page

Redirect to login page unless a user is logged in

How to redirect to login page if the user is not logged in?

Laravel, redirect to specific login page if user is not authenticated

redirect user to a a page after he login

ActiveAdmin Gem: Redirect user to login page if not logged in

Redirect to login page if user is not logged in wordpress?

How to redirect a user to login page in mvc

In Django, after login redirect the user to the previous page

redirect to login page when user is not authenticated

How unauthorize access redirect user to login page

redirect logged in user if they went to login page

Best way to make a login page with HSQLDB - Java

Redirect MVC URL to login page if user has not login

proper way to redirect user to their origin page?

Redirect to login page if user goes straight to home page without logging in

ZF2 redirect to login page on every page if user is not logged in

Joomla login page, if user is already logged redirect to home page

How to redirect already authenticated user from login page to home page

Django: best way to redirect empty results pages to root page

What is the best way to redirect a page using React Router?

How To Redirect Logged In User To My Account If They Visit To Login & Signup Page?

Django: redirect the user from the login page if he has already logged in

How to redirect user to login page from middleware in django rest framework?

How to redirect http://127.0.0.1:8000/ to login page if the user is not logged in

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive