Tuesday, 17 September 2013

Login and session maitainance and logout

$("#btnLogin").click(function () {
                var u = $('#UserName').val();
                var p = $('#Password').val();


                $.ajax({
                    url: '@Url.Action("UserLogin""Home")',
                    data: { "username": u, "password": p },
                    type: 'POST',
                    dataType: 'json',
                    success: function (response) {
                        user = response.User;
                        if ($.isEmptyObject(user)) {
                            //alert("Invalid Username of password");
                            $('#loginMsg').text("Invalid");
                        }
                        else {
                            $.each(user, function () {
                                if (this.Type == "Admin") {
                                    $('#loginMsg').text("Success" + this.Type);

                                   window.location = "../Admin/Admin?User=" + this.Username;
                                }
                                else if (this.Type == "College") {
                                    window.location = "../College/Index?User=" + this.Username+"&Code="+this.code;
                                }

                            });

                        }
                    },
                    error: function (xhr, status, error) {
                    }
                });

            });

In controller

public ActionResult Index()
        {
            Session["user"] = null;
            Session.Abandon();
            return View();
        }

[HttpPost]
        public ActionResult UserLogin()
        {

            string u = Request["UserName"];
            string p=Request["Password"];
            Session["user"] = "Home";
         
            return Json(new { User = obj.UserLogin(u,p) });
        
        }

In college index page

public ActionResult Index()
        {
             if (Session["user"] == null)
            {
                return RedirectToAction("Index","Home");
            }
            else if (Session["user"].ToString() == "Home")
            {
                string u = Request["User"];
                string code = Request["Code"];
                Session["user"] = u;
                Session["LoginCode"] = code;
                ViewBag.session = u;
                ViewBag.LoginCode = code;
            }
            else if (Session["user"] != null)
            {
                ViewBag.session = Session["user"];
                ViewBag.LoginCode = Session["LoginCode"];
            }
           
          
            return View();
        }

In the index page of college get the viewbag

          var logincode = '@ViewBag.LoginCode';

for logout go to the home page of the site and

public ActionResult Index()
        {
            Session["user"] = null;
            Session.Abandon();
            return View();
        }

No comments:

Post a Comment