I needed to create a simple fast login form for an internal company project I’m working on. I saw this tut on how to do it using vb – looked really fast and easy. I ran it through a converter to change it to C# but when I load the page, this is the error I get:
CS0103: The name 'Session' does not exist in the current context
With the following code:
<script runat="server">
public void Login(object s, EventArgs e)
{
if (tbUserName.Text == "admin" & tbPassword.Text == "admin")
{
Session("Admin") = true;
Response.Redirect("Dashboard.aspx");
}
else
{
Session("Admin") = false;
litLogin.Visible = true;
litLogin.Text = "<p>Sorry you have provided incorrect login details.</p>";
}
}
</script>
EDIT Adding brackets helps on the login page, but on the page im trying to protect you have to check for the session like this:
<form id="form1" runat="server">
<script runat="server">
protected void Page_Load(object sender, System.EventArgs e)
{
if (Session["Admin"] != true)
{
Response.Redirect("Login.aspx");
}
}
</script>
And it throws CS0019: Operator '!=' cannot be applied to operands of type 'object' and 'bool'
In C#, you need to use
[and]for indexers.In other words, replace
With
In response to your edit:
Should be cast to bool, so you should try:
(No need to compare a boolean value with true/false)