'ASP.Net Cookie, my session values are null
I have problem with my cookie and session. I use cookies to move data between pages, and sessions only to check whether a session is logged or not. But after assigning them, they get a null value on the page I redirect with response.redirect. When you set the cookieless setting to true via the web config, the session works, but in this case, the session id appears on the url and the cookie operations still do not work.
Error I get for cookies and sessions
Object reference not set to an instance of an object.
my login code
SqlConnection baglan;
string ConnectionString = ConfigurationManager.ConnectionStrings["lisaeduConnectionString"].ConnectionString;
baglan = new SqlConnection(ConnectionString);
SqlCommand komut;
SqlDataReader veriOku;
komut = new SqlCommand("SELECT * FROM users WHERE CONVERT(VARCHAR, username)=@username and CONVERT(VARCHAR, userpassword)=@password", baglan);
komut.Parameters.Add("@username", System.Data.SqlDbType.NVarChar);
komut.Parameters["@username"].Value = TextBox1.Text;
komut.Parameters.Add("@password", System.Data.SqlDbType.NVarChar);
komut.Parameters["@password"].Value = TextBox2.Text;
baglan.Open();
veriOku = komut.ExecuteReader();
if (veriOku.Read())
{
Session["kullanici"]=kad.ToString();
Response.Redirect("~/admins/adminmain.aspx");
}
else
{
TextBox1.Text = "Kullanıcı adı veya şifre hatalı";
}
baglan.Close();
komut.Dispose();
my sessşon control code
if (Session["kullanici"] != null)
{
}
else
{
Response.Redirect("~/Admin");
}
my Cookie code
RepeaterItem item = (sender as Button).NamingContainer as RepeaterItem;
Response.Cookies.Add(new HttpCookie("no", (item.FindControl("Label1") as Label).Text)
{
HttpOnly = true,
Secure = true,
});
Response.Redirect("adminkampanyaduzenle.aspx");
and cookies code2
DropDownList1.Items.Add("Yayında");
DropDownList1.Items.Add("Yayın Dışı");
string connString = ConfigurationManager.ConnectionStrings["lisaeduConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(connString);
SqlCommand comm = new SqlCommand("SELECT * FROM banner where bannerid='" + Request.Cookies["no"].Value + "'", conn);
SqlDataReader reader;
try
{
//Bağlantımı açıyorum.
conn.Open();
//Reader nesnem için sql komutumu çalıştırıyorum
reader = comm.ExecuteReader();
//Repeater nesnemi verime bağlama işlemi yapıyorum.
while (reader.Read())
{
Image1.ImageUrl = "../" + reader["bannerimg"].ToString();
TextBox1.Text = System.Net.WebUtility.HtmlDecode(reader["bannerbaslik"].ToString());
String metin= System.Net.WebUtility.HtmlDecode(reader["banneraciklama"].ToString());
//metin=metin.Replace("\"","'");
ckeditor.Text = metin;
DropDownList1.SelectedValue = System.Net.WebUtility.HtmlDecode(reader["bannerdurum"].ToString());
}
//Reader nesnemi kapatıyorum.
reader.Close();
}
//hata olursa vereceğim mesaj.
catch
{
}
//Bağlantımı kapatıyorum
finally
{
conn.Close();
}
and my web config
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="lisaeduConnectionString" connectionString="Data Source=****;Initial Catalog=*****;Integrated Security=False;User ID=****;Password=****;" providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime targetFramework="4.0" requestValidationMode="2.0" maxRequestLength="1048576" executionTimeout="3600" />
<globalization requestEncoding="iso-8859-9" responseEncoding="iso-8859-9" culture="tr-TR" uiCulture="tr-TR" fileEncoding="iso-8859-9" />
<sessionState timeout="60" mode="InProc" ></sessionState>
<customErrors mode="Off" defaultRedirect="Default.aspx" />
<pages validateRequest="false" controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID">
<controls>
<add tagPrefix="ajaxToolkit" assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" />
</controls>
</pages>
<httpCookies httpOnlyCookies="true" requireSSL="true" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login.aspx" timeout="2880" />
</authentication>
<profile defaultProvider="DefaultProfileProvider">
<providers>
<add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
</providers>
</profile>
<membership defaultProvider="DefaultMembershipProvider">
<providers>
<add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
</providers>
</membership>
<roleManager defaultProvider="DefaultRoleProvider">
<providers>
<add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
</providers>
</roleManager>
</system.web>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
</configuration>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|