Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using session in my login section. I tried to get data from database and store into a session, session store the value perfectly but session is not provide the data on different page when I used session to get data.

What I have tried:

This is my function where i got the data in session.
public ActionResult Login(LoginViewModel model)
         {

         try
         {

             if (ModelState.IsValid)
             {
                 using (DBClass context = new DBClass())
                 {

                     context.AddParameter("@loginid", model.UserName);
                     context.AddParameter("@Password", model.Password);

                     DataTable dtLogin = context.getData("admincheck", CommandType.StoredProcedure);
                     if (dtLogin.Rows.Count > 0)
                     {

                         Session["Adminname"] = Convert.ToString(dtLogin.Rows[0]["LoginId"]);

                         Session["UserRole"] = Convert.ToString(dtLogin.Rows[0]["UserRole"]);

                         if (Convert.ToString(Session["UserRole"])=="Admin")
                         {

                             return Content("<script language='javascript' type='text/javascript'> window.location.href ='http://localhost:32607/Admin/Adminhome';</script>");

                         }
                         else
                         {
                             return Content("<script language='javascript' type='text/javascript'> window.location.href ='http://localhost:32607';</script>");

                         }
                     }

                     else if(dtLogin.Rows.Count==0)
                     {

                           return Content("<script language='javascript' type='text/javascript'> alert('You are not a valid user.'); window.location.href ='http://localhost:32607/Admin';</script>");

                     }
                     else
                     {


                         return Content("<script language='javascript' type='text/javascript'>alert('Username and Password provided is incorrect.'); window.location.href ='http://localhost:32607/Admin/adminlogin'; </script>");

                     }
                 }
             }
             else
             {


                 return Content("<script language='javascript' type='text/javascript'>alert('Username and Password provided is incorrect.'); window.location.href ='http://localhost:32607/Admin/adminlogin'; </script>");
             }
         }




Session is working perfectly here but when i used this session on different page like my view at that it is not working.
In the below example the session is not fetch the data.

<li>
               <a href="#">
                   <span style="color:white;">
                       Hi @Session["Adminname"]
                   </span>
               </a>
           </li>

or
@if(convert.ToString(Session["Adminname"])!=null)
{
   //my content
}
Posted
Updated 23-Nov-20 21:04pm
v3
Comments
Richard Deeming 24-Nov-20 2:35am    
You seem to be storing users' passwords in plain text. Don't do that:
Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]
Member 14743579 24-Nov-20 2:39am    
I know about it but my current problem is session if you have any solution about my problem then please give me.

1 solution

I got the solution, Done some changes in my code and it's working perfectly..
 
Share this answer
 
Comments
Ravi Nimje 2021 6-Apr-22 2:02am    
what kind of changes have you done to your code please share them.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900