首页 > 解决方案 > 如何使用 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();
    }

输出截图:

在此处输入图像描述

标签: c#sqlasp.net-mvc

解决方案


当您使用会话时,该会话在整个站点中可用,具体取决于您在 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();
}

推荐阅读