How to get a single HelperClass.IsLoggedIn () method for an entire ASP.NET web project?

This is a common necessity that I have on every page:

if (session["LoggedIn"] == null || ((bool)session["LoggedIn"] != true))
....//user is not logged in. return.

I was wondering if there is a way to create a Helper class with the method signature bool IsLoggedIn()and call this method from the page so that it can automatically check whether the page from which it was called was session["LoggedIn"]set to true? Something like that:

class Helper
{
  public bool IsLoggedIn()
  {
    System.Web.UI.Page page = ***FindCallerPageSomeHow***();
    if(page.session["LoggedIn"] == null || ((bool)page.session["LoggedIn"] != true))
      return false;
    return ((bool)page.HpptContext.Session["LoggedIn"] == true);
  }
}

Of course, I could try to implement an interface for each codebehind class, but this repeats. Also, I could go in HttpContextfor IsLoggedIn, but it's a bit of a nuisance ..

Any ideas? Is there an easy-to-implement template for this?

+3
source share
4 answers

, - .

public class myPage : System.Web.UI.Page
{

    public void myPage()
    {
    }

    public bool IsLoggedIn()
    {
        System.Web.UI.Page page = ***FindCallerPageSomeHow***();
        if(page.session["LoggedIn"] == null || ((bool)page.session["LoggedIn"] != true))
            return false;
        return ((bool)page.HpptContext.Session["LoggedIn"] == true);
    }
}

public partial myAspxPage : myNamespace.myPage
{

}

, ValidUser, ( ). null, . , , . , , .

EDIT: .

+1

, , , . formsauthentication .

Btw, :

public static bool IsSignedIn(this HttpSessionState session) {
    //Use the session to check if the user is signed in
}

:

if(Page.Session.IsSignedIn()) {
    //Code
}
+2

? , , , , . .

0

BasePage, System.Web.UI.Page. codebehind BasePage.

BasePage , IsLoggedIn(), .

0

Source: https://habr.com/ru/post/1729645/


All Articles