c# - 如何使用 ASP.NET MVC 在配置文件中显示当前登录用户详细信息
问题描述
我想使用 Session 在用户配置文件中显示用户详细信息,但它不能以任何其他方式工作,请建议我,我正在使用 ASP.NET MVC。
登录类:
[HttpGet]
public ActionResult Login()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(UserLogin signinentity, Userdb sessin, string ReturnUrl)
{
string message = "";
using (var context = new ApplicantDataEntities())
{
var umail = context.Userdbs.Where(x => x.u_Email == signinentity.u_Email).FirstOrDefault();
if (umail != null)
{
if (string.Compare(PassHash.Hash(signinentity.u_Password), umail.u_Password) == 0)
{
int timeout = signinentity.Rememberme ? 52600 : 20; // 525600 min=1 year
var ticket = new FormsAuthenticationTicket(signinentity.u_Email, signinentity.Rememberme, timeout);
string encrypted = FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
cookie.Expires = DateTime.Now.AddMinutes(timeout);
cookie.HttpOnly = true;
Response.Cookies.Add(cookie);
if (Url.IsLocalUrl(ReturnUrl))
{
return Redirect(ReturnUrl);
}
else
{
Session["firstname"] = sessin.u_Firstname;
Session["lastname"] = sessin.u_lastname;
Session["discription"] = sessin.u_dscrptn;
Session["dob"] = sessin.u_dob;
Session["mail"] = sessin.u_Email;
Session["gender"] = sessin.u_Gender;
Session["passs"] = sessin.u_Password;
Session["profilepic"] = sessin.u_ProfilePic;
Session["usertype"] = sessin.u_type;
return RedirectToAction("Index", "Dashboard");
}
}
else
{
message = "Invalid credentials";
}
}
else
{
message = "User with this email not exists";
}
ViewBag.Message = message;
return View();
}
}
仪表板控制器:
[Authorize]
public ActionResult Index(Userdb sessin)
{
Session["firstname"] = sessin.u_Firstname;
Session["lastname"] = sessin.u_lastname;
Session["discription"] = sessin.u_dscrptn;
Session["dob"] = sessin.u_dob;
Session["mail"] = sessin.u_Email;
Session["gender"] = sessin.u_Gender;
Session["passs"] = sessin.u_Password;
Session["profilepic"] = sessin.u_ProfilePic;
Session["usertype"] = sessin.u_type;
ViewBag.firstname = Session["firstname"];
ViewBag.lastname = Session["lastname"];
ViewBag.discription = Session["discription"];
ViewBag.dob = Session["dob"];
ViewBag.mail = Session["mail"];
ViewBag.gender = Session["gender"];
ViewBag.passs = Session["passs"];
ViewBag.profilepic = Session["profilepic"];
ViewBag.usertype = Session["usertype"];
return View();
}
输出截图:
解决方案
当您使用会话时,该会话在整个站点中可用,具体取决于您在 Web 配置文件中提供的时间。为什么要在源操作和目标操作中设置值?
我使用 TempData 来提高服务器的内存以在读取后释放内存。
如下更改您的以下代码。
[HttpGet]
public ActionResult Login()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(UserLogin signinentity, Userdb sessin, string ReturnUrl)
{
string message = "";
using (var context = new ApplicantDataEntities())
{
var umail = context.Userdbs.Where(x => x.u_Email == signinentity.u_Email).FirstOrDefault();
if (umail != null)
{
if (string.Compare(PassHash.Hash(signinentity.u_Password), umail.u_Password) == 0)
{
int timeout = signinentity.Rememberme ? 52600 : 20; // 525600 min=1 year
var ticket = new FormsAuthenticationTicket(signinentity.u_Email, signinentity.Rememberme, timeout);
string encrypted = FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
cookie.Expires = DateTime.Now.AddMinutes(timeout);
cookie.HttpOnly = true;
Response.Cookies.Add(cookie);
if (Url.IsLocalUrl(ReturnUrl))
{
return Redirect(ReturnUrl);
}
else
{
TempData["UserProfileData"] = umail;
return RedirectToAction("Index", "Dashboard");
}
}
else
{
message = "Invalid credentials";
}
}
else
{
message = "User with this email not exists";
}
ViewBag.Message = message;
return View();
}
}
和仪表板控制器中的索引操作:
[Authorize]
public ActionResult Index()
{
Userdb userdb = (Userdb)TempData["UserProfileData"];
ViewBag.firstname = userdb.firstname;
ViewBag.lastname = userdb.lastname;
ViewBag.discription = userdb.discription;
//.......................
return View();
}
推荐阅读
- c++ - 为什么 GRPC 会生成模板类?
- javascript - 如何在反应中为我的输入添加条件?
- azure-ad-b2c - 使用 Azure AD B2C 的多个应用程序的最佳实践
- javascript - 按键重新排序 Map 对象
- python - 如何计算平均真实范围 - scipy
- python - 数组行被填充而不是单个元素
- python - 将变音符号或修饰符变音符号 Unicode 字符转换为“组合形式”
- python - 如何使用 tf.GradientTape 训练分支神经网络?
- windows - 工具栏的 ISO 标准图标 (Windows)
- php - 如果图像在高级用户中,我想添加高级图像/文本