I would like create page (in c# language) where I could type login, password and after checking checkbox could save "mypassword" to the computer cookies. After clicking button, script should be check my login and my password and save "mypassword" to the session cookies (to receive on page "admin.aspx") and redirect to "admin.aspx". If "password" was already written in computer cookies or in server cookies (when I have already sign in this session). But I receive the error: ‘An unhandled exception occurred during the execution of the current web request. System.NullReferenceException: Object reference not set to an instance of an object’.
Here whole code of the script:
<%@ Page Language="c#"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><script type="text/c#" runat="server" language="c#">
// Private variables:
private string clogin;
private string cpassword;
//When page load:
protected void Page_Load(object sender, EventArgs e)
{
// Content of private variables, correct login and password:
clogin = "szmitek";
cpassword = "mypasswod";
// If ‘mypassword’ (content of variable cpassword) was already written in computer or session cookies (it cause error; line 13):
if (Request.Cookies["login"]["password"] == cpassword || Session["password"] == cpassword)
{
// Automated redirecting:
Response.Redirect("admin.aspx");
}
}
// After clicking button:
protected void SubmitBtn_Click(object sender, EventArgs e)
{
// If text from textboxes login and password was correct:
if (login.Text == clogin && password.Text == cpassword)
{
// Adding ‘mypassword’ to session cookies:
Session["password"] = cpassword;
// Adding ‘mypassword’ to computer cookies when checkbox is checked:
if (CheckBox.Checked == true)
{
Response.Cookies["login"].Domain = "szmitek.winweb.pl";
Response.Cookies["login"]["password"] = cpassword;
}
// Redirecting:
Response.Redirect("admin.aspx");
}
else
{
// Redirecting when text from texboxes is not correct:
Response.Redirect("fail.aspx");
}
} </script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Administracja - logowanie</title><link rel="stylesheet" type="text/css" href="../format.css" /></head><body><h1>Zaloguj się</h1><div class="center">
// Form:
<form runat="server" method="post" action="index.aspx">Login:
<asp:TextBox ID="login" runat="server"></asp:TextBox><br />Hasło:
<asp:TextBox ID="password" runat="server" TextMode="Password"></asp:TextBox><br />
// Checkbox. If it is checked, ‘mypassword’ will be save in computer cookies:
<asp:CheckBox ID="CheckBox" Text="Loguj automatycznie w tym systemie" runat="server" /><br /><asp:Button id="Button1" runat="server" text="Zaloguj się" onclick="SubmitBtn_Click" /></form></div></body></html>
How to resolve this problem? Am I receiving session cookies in correct way (Session[“passwod”]).
And this is fragment of error message:
…
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
|
Line 11: cpassword = "bcvnma";
Line 12:
Line 13: if (Request.Cookies["login"]["password"] == cpassword || Session["password"] == cpassword)
Line 14: {
Line 15: Response.Redirect("admin.aspx"); |
…
Stack Trace:
|
[NullReferenceException: Object reference not set to an instance of an object.]
ASP.admin_index_aspx.Page_Load(Object sender, EventArgs e) in i:\Users\Administrator\Documents\My Web Sites\Kamil Szmit (szmitek)\admin\index.aspx:13
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +34
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061 |
…
What cause the problem? How to receive session cookie (added by ‘Session[“password”] = cpassword’) in good, correct way?