如何使用Aspstate Db获取会话值,我在哪里使用Sqlserver模式存储会话详细信息 [英] How Can I Get The Session Value Using The Aspstate Db, Where I Am Storing The Session Details Using Sqlserver Mode

查看:66
本文介绍了如何使用Aspstate Db获取会话值,我在哪里使用Sqlserver模式存储会话详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是Web.config文件中的配置。







This is what configuration in the Web.config file.



<sessionState mode="SQLServer" customProvider="DefaultSessionProvider">
      <providers>
        <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="SessionSqlCon" cookieless="false" timeout="10" />
      </providers>
    </sessionState>









会话详细信息已存储在数据库中称为ASPState,但我无法从数据库访问会话值。



我试过以下代码,但我认为它不正确。









public ActionResult Home()

{

ViewBag.Result = Session [username]。ToString();



SqlCommand cmd = new SqlCommand(从ASPStateTempSessions中选择SessionId,con);

object obj;



byte [] bytdata = new byte [50];

con.Open();

SqlDataReader dr = cmd.ExecuteReader();



if(dr.HasRows)

{

while(dr.Read())

{

obj = dr [SessionId]。ToString();

// string obj = dr [SessionId]。ToString();

bytdata = System.Text.Encoding.UTF8.GetBytes(obj.ToString());

System.IO .MemoryStream ms = new System.IO.MemoryStream(bytdata);

BinaryFormatter bin = new BinaryFormatter();

//bin.Serialize(ms,bytdata);

ms.Seek(0,SeekOrigin.Begin);

object ob = bin.Deserialize(ms);

ViewBag.Data = ob;



//字符串会话= Convert.ToString(bin.Deserialize(ms));

//字符串会话= BitConverter.ToString( bin.Deserialize(ms));

}



}



return View();

}





The session details have been stored in the DB called ASPState, but i am not able to access the session value from the DB.

I have tried following code, but i think its not right way.




public ActionResult Home()
{
ViewBag.Result = Session["username"].ToString();

SqlCommand cmd = new SqlCommand("select SessionId from ASPStateTempSessions", con);
object obj;

byte[] bytdata = new byte[50];
con.Open();
SqlDataReader dr = cmd.ExecuteReader();

if (dr.HasRows)
{
while (dr.Read())
{
obj = dr["SessionId"].ToString();
//string obj = dr["SessionId"].ToString();
bytdata = System.Text.Encoding.UTF8.GetBytes(obj.ToString());
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytdata);
BinaryFormatter bin = new BinaryFormatter();
//bin.Serialize(ms, bytdata);
ms.Seek(0, SeekOrigin.Begin);
object ob = bin.Deserialize(ms);
ViewBag.Data = ob;

//string session = Convert.ToString(bin.Deserialize(ms));
//string session = BitConverter.ToString(bin.Deserialize(ms));
}

}

return View();
}

推荐答案

永远不会!通过直接访问您定义的数据库来访问会话数据...让IIS / ASP.NET为您编写和读取数据...

使用您拥有的Session对象...

https:// msdn。 microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate(v=vs.110).aspx [ ^ ]
Never ever! access session data by direct access of the database you defined...Let IIS/ASP.NET write and read the data for you...
Use the Session object you have...
https://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate(v=vs.110).aspx[^]


这篇关于如何使用Aspstate Db获取会话值,我在哪里使用Sqlserver模式存储会话详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆