I have a session with an object 'User', and I'm trying pass the session object to BLSecurity.cs. This class uses the User object to check the login status and return a boolean.
The problem is that 'Gebruiker', not matter what, stays null.
BLSecurity.cs
public class BLSecurity {
private wk_Gebruiker gebruiker;
public wk_Gebruiker Gebruiker{
get { return gebruiker; }
set { gebruiker = value; }
}
public BLSecurity() {
}
public bool GebruikerIsIngelogd() {
return Gebruiker != null;
}
public bool GebruikerIsAdmin() {
return Gebruiker != null && Gebruiker.gebruikerniveauID == 1;
}
}
In the Master file from my asp project I do the following in the Load function
protected void Page_Load(object sender, EventArgs e) {
BLSecurity BLSecurity = new BLSecurity();
wk_Gebruiker gebruiker = (wk_Gebruiker)Session["gebruiker"];
if (gebruiker != null) {
LabelTest.Text = gebruiker.voornaam;
} else {
LabelTest.Text = "no name";
}
BLSecurity.Gebruiker = gebruiker;
...
}
When a user is logged in, he sees his surname with the labelTest and when he logs out, he sees the 'no name'. So that part works, but there must be something wrong with passing it to BLSecurity.
Thank you for your help!
Ah, I found the problem.
The problem was that I created a new instance of BLSecurity when I needed the function GebruikerIsIngelogd();
So I had in my Load function the set part
protected void Page_Load(object sender, EventArgs e) {
BLSecurity BLSecurity = new BLSecurity();
BLSecurity.Gebruiker = (wk_Gebruiker)Session["gebruiker"];
SomeFunction();
}
And in the function SomeFunction();
I needed those methods.
SomeFunction();
private void SomeFunction(){
BLSecurity BLSecurity = new BLSecurity(); //here lies the problem
if(BLSecurity.IsLoggedIn()){
...
} else {
...
}
}
So the problem is this new instance of BLSecurity in DoSomething()
The solution was to move the instance above the Page_Load()
public partial class WineKeeper : System.Web.UI.MasterPage {
private BLSecurity BLSecurity = new BLSecurity();
protected void Page_Load(object sender, EventArgs e) {
BLSecurity.Gebruiker = (wk_Gebruiker)Session["gebruiker"];
SomeFunction();
}
}
This way I won't be overridden which would result in null.