Skip to content
Advertisement

How to show current login User details in profile using ASP.NET MVC

I want to show user details on User Profile using Session but it is not working any other way kindly suggest me, I’m using ASP.NET MVC.

Login class:

[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();
    }
}

Dashboard controller:

    [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();
    }

Screenshot of output:

enter image description here

Advertisement

Answer

When you use a session, that session is available throughout the site based on how long you have given it in the web config file. And why do you set values in both source and destination action?

I used TempData to improve the memory of the server to free up memory after reading.

Change your following code as follows.

[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();
    }
}

and Index Action in Dashboard controller:

[Authorize]
public ActionResult Index()
{
    Userdb userdb = (Userdb)TempData["UserProfileData"];
    ViewBag.firstname = userdb.firstname;
    ViewBag.lastname = userdb.lastname;
    ViewBag.discription = userdb.discription;
    //.......................
    return View();
}

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement