[英]Session value returned null in mvc5 asp.net
I have login controller and in this i get my session values我已经登录 controller 并在此我得到我的 session 值
[HttpPost]
public ActionResult Login(Models.AdminUsers adminUsers)
{
try
{
if (Session["iUserId"] != null || GetCookie("iUserId") != null)
{
return Redirect("/Home/Index");
}
if (ModelState.IsValid)
{
using (Data.DataClassesDataContext dc = new Data.DataClassesDataContext())
{
var resultUsers =
(from tableAdminUsers in dc.AdminUsers
where
tableAdminUsers.cEmail == adminUsers.cEmail &&
tableAdminUsers.cPassaword == new Class.AesCryption().Encryption(adminUsers.cPassaword) &&
tableAdminUsers.iActive == 1
select new Models.AdminUsers
{
iUserId = tableAdminUsers.iUserId,
cEmail = tableAdminUsers.cEmail,
cUserName = tableAdminUsers.cUserName,
cUserSurname = tableAdminUsers.cUserSurname,
cImage = tableAdminUsers.cImage
}).FirstOrDefault();
if (resultUsers != null)
{
if (adminUsers.lBeniHatirla == false)
{
Session.Add("iUserId", resultUsers.iUserId);
Session.Add("cEmail", resultUsers.cEmail);
Session.Add("cUserName", new Class.TextLowerAndFirstUpper().Send(resultUsers.cUserName));
Session.Add("cUserSurname", resultUsers.cUserSurname.ToUpper());
Session.Add("cImage", resultUsers.cImage);
}
else
{
CreateCookie("iUserId", resultUsers.iUserId.ToString());
CreateCookie("cEmail", resultUsers.cEmail);
CreateCookie("cUserName", new Class.TextLowerAndFirstUpper().Send(resultUsers.cUserName));
CreateCookie("cUserSurname", resultUsers.cUserSurname.ToUpper());
CreateCookie("cImage", resultUsers.cImage);
}
return Redirect("/Home/Index");
}
else
{
ViewBag.iSonuc = -7;
}
}
}
else
{
ViewBag.iSonuc = -6;
}
}
catch (Exception Ex)
{
new Class.Log().Hata("AdminUsers", "AdminUsers_Post", Ex.Message);
}
return View();
}
And i want to control to session in another controller but my session value return null.我想在另一个 controller 中控制到 session 但我的 session 值返回 Z37A6259CC0C19DAE0BD99。 My control like this:我的控制是这样的:
if (Session["iUserId"] == null && GetCookie("iUserId") == null)
{
return Redirect("/AdminUsers/Login");
}
int iUserLogin = 0;
if (Session["iUserId"] != null && Convert.ToInt32(Session["iUserId"]) > 0)
{
iUserLogin = Convert.ToInt32(Session["iUserId"]);
}
else if (GetCookie("iUserId") != null && Convert.ToInt32(GetCookie("iUserId")) > 0)
{
iUserLogin = Convert.ToInt32(GetCookie("iUserId"));
}
if (Session["iUserId"] == null && GetCookie("iUserId") == null) this row return true and redict to login page again.But i getting cookie correctly Why session value return null? if (Session["iUserId"] == null && GetCookie("iUserId") == null) 此行返回 true 并再次重定向到登录页面。但是我正确获取 cookie 为什么 session 值返回 Z37A6259CC0C1DAE029CC0C1DAE09?
Where am I making mistakes?我在哪里犯错? Can you help me?你能帮助我吗?
If it is a .net core, use httpcontext.如果是 .net 内核,使用 httpcontext。 You can solve it using a distribution cache such as Redis.您可以使用诸如 Redis 之类的分发缓存来解决它。 https://docs.microsoft.com/tr-tr/aspnet/core/fundamentals/app-state?view=aspnetcore-5.0 https://docs.microsoft.com/tr-tr/aspnet/core/fundamentals/app-state?view=aspnetcore-5.0
If you want to develop a User Manager Use a thirty part nuget like Jwt .如果要开发用户管理器,请使用三十部分nuget ,例如 Jwt 。 What it does is sso logic gives you a token for the user you use it它的作用是 sso 逻辑为您使用它的用户提供一个令牌
Try using Session with HttpContext as below:-尝试将 Session 与 HttpContext 一起使用,如下所示:-
HttpContext.Current.Session["iUserId"]=value;
Alternativly, you can try using TempData rather then Session.或者,您可以尝试使用 TempData 而不是 Session。
TempData["iUserId"]=value;
There is a localization function that I use for language change, in this function I am changing the value of the culture using thread, I realized that this function resets my session value. There is a localization function that I use for language change, in this function I am changing the value of the culture using thread, I realized that this function resets my session value.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.