0

I am having a modal popup to create event and i want to allow only login user to see that page, save event and fetch event all are using json, but when i am going back after logout, session value is still present and all actions are getting performed, until i donot refresh the page, i want that no action should happen and session value should be cleared when i logout

    public ActionResult Index()
    {

        if(Session["UserID"]==null)
        {
            return RedirectToAction("Index2","Login");
        }
        else
        {
            TempData["usersession"] = Session["UserID"].ToString();
        }
            return View();


    }

<label id="session">@TempData["usersession"]</label>

  //Javascript and Json



$(document).ready(function () {
            username = $('#session').text();
});
 function SaveEvent(data) {
                alert(username);
                    $.ajax({
                        type: "POST",
                        url: '/home/SaveEvent',
                        data: data,
                        success: function (data) {
                            if (data.status) {
                                //Refresh the calendar
                                fetchEvent();
                                $('#myModalSave').modal('hide');
                                //alert(username);
                            }
                        },
                        error: function () {
                            alert('failed');
                        }
                    });

While i am trying to alert username when i click on save it still showing the session value

1 Answers1

0

have you tried this?

 Session.Abandon(); // The Abandon method destroys all the objects stored in a Session object and releases their resources. 

    Session.Remove("YourItem"); //just removes current values

    Session.Clear();// just removes all values

https://stackoverflow.com/a/5330288/7262120

public class VerifyUserAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var user = filterContext.HttpContext.Session["UserID"];
        if (user == null)
            filterContext.Result = new RedirectResult(string.Format("/User/Login?targetUrl={0}",filterContext.HttpContext.Request.Url.AbsolutePath));
    }
}
[VerifyUserAttribute]
 public ActionResult Index()
    {

        if(Session["UserID"]==null)
        {
            return RedirectToAction("Index2","Login");
        }
        else
        {
            TempData["usersession"] = Session["UserID"].ToString();
        }
            return View();


}
Arvind Maurya
  • 910
  • 12
  • 25
  • yes it worked, can u tell how should i save the session value in database, because that field will be filled automatically – sunny kumar Jan 17 '19 at 05:49
  • i got you some what, can you please elaborate it? – Arvind Maurya Jan 17 '19 at 06:47
  • i just want that after loggin in, the session value which is having username , should also be saved with the other fields, but as the fields in the view are all textboxfor , and this session value is already filled, so how should i save this value in the database with all the other values, – sunny kumar Jan 17 '19 at 07:10
  • if you are using model, first save username to model and then save it to database – Arvind Maurya Jan 17 '19 at 08:03
  • bro i am passing the session variable to model in a label. but how to save that model by passing its value to controller if that it is directly binded with the model – sunny kumar Jan 17 '19 at 09:07