Cookies in MVC

Creating Cookie in Clint side Browser .


 HttpCookie cookie = new HttpCookie("Cookie Name");//cookie is object of HttpCookie
                cookie.Value = md.Userid + md.password;//Set value of cookie
                HttpContext.Response.Cookies.Add(cookie);//Add cookie in Browser
                cookie.Expires.AddDays(1);    //Set Time of Cookie Expiring Time


Expiring Cookie .


[Route("out")]
        public ActionResult logout()
        {
            HttpCookie cookie = new HttpCookie("LoginCookie");
            if (Request.Cookies["LoginCookie"] != null)
            {
                HttpCookie myCookie = new HttpCookie("LoginCookie");
                myCookie.Expires = DateTime.Now.AddSeconds(1);
                Response.Cookies.Add(myCookie);
            }
           
            Session["username"] = null;
            Session["password"] = null;
            return RedirectToAction("Login");
        }

Comments

Popular posts from this blog

Search Record From Table Using jQuery in Asp.net core MVC

How To insert Radio Button Data in DataBase -- MVC

Toolbox Webparts Controls