Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have following code.

 User Controller
[HttpGet]
      public ActionResult Login()
      {
          return View();
      }
 
      [HttpPost]
      public ActionResult Login(Models.User user)
      {
          var qery = db.Users.Where(q => q.UserName == user.UserName && q.Password == user.Password);
          if (ModelState.IsValid)
          {
              if (qery.Any())
              {
                  if (qery.FirstOrDefault().Role.Role1.ToLower()=="admin")
                  {
                      return RedirectToAction("Index", "user");
                  }
                  else
                  {
                      return RedirectToAction("Details", "user", new { id=qery.FirstOrDefault().Id});
                  }
              }
          }
          return View(user);
      }

 

now my Question is i want to use Session in Login And Logout Page.
and i m Using Mvc CRUD operation.(i.e Entity framework)
Posted
Comments
F-ES Sitecore 11-Mar-15 7:28am    
I'm going to assume you want to use the Session to track if a user is logged on. You could do this by storing the user id in the session and checking if there is a user id when you need to validate a user is logged in. However you're better implementing asp.net Identity as that has a lot of this stuff written for you, and it is cookie based rather than session based so it is easier to persist people's logins.
Member 11438493 12-Mar-15 3:33am    
thank you so much for your suggestion but as told by my mentor i want to use session.

Session variables have nothing to do with the CRUD, or Entity Framework or any other such process that you have to trigger, they are just variables (as name states) that one can use to store session-based data for a client. A CRUD operation is based on objects; specifically in a data source for CREATING, READING, UPDATING, DELETING the object.

Take an example of session-token, username, and all other user-based data that you want to store but want to get removed as soon as the session ends; browser window closed, user terminates the session, or you find something that should not be going on and you clear the session yourself. In these cases you use the Session variables. They're something like this,

C#
Session["variable_name"] = "My name is Afzaal Ahmad Zeeshan";


Voila! Variable has been created, now to access it, you do the following thing...

C#
if(Session["variable_name"] != null) {
   // Always check whether the variable exists or not
   // Following has a string type; because you assigned string value
   var name = Session["variable_name"];
}


You can use different type of data too; such as struct or class, just cast them. Now I have a question, where do you want to use the Session variable? I don't find any place in your code where you might possibly want to be storing anything at all...

For more on using the Session variable please read the Session variable section in this article[^] of mine... And for a background about CRUD you can read this article[^] of mine (based on SQL Server). A personal tip would be to not use the Session variables in CRUD operations unless required by design.
 
Share this answer
 
v2
Thank you so much for all who give suggestion.

i need one more suggestion.

I m Using Admin and user Role.

if user is login using session then he should not access to private url.

for eg: i have user and when he login then URL is
localhost4584:/user/Details/2 .

But this user can edit url and he can go back to user Private data which is restricted to see by user.
for eg.localhost4584:/user and user can see confidential data.


And here my Question is how to prevent user to assess private url.
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 12-Mar-15 4:51am    
Please start a new thread.

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