简体   繁体   中英

Create an application in ASP.NET MVC approach which has the following functionalities:

• Create model, controller and the views given below. • On the submit button click the data about employee should be stored in cookie. • When the user clicks on “Retrieve” ActionLink, the cookie values should be read and displayed on EmpDtl.cshtml view.

The only reason for this question is that I do not know how to Retrieve and Store data in cookies can someone show me how to do it any generic code which retrieve and store data in cookies and if possible with explanation because some of the things have not cleared to me like what is expirationMinutes and how much time a session will be created.

Image of Scenario

Something like the following will store a cookie with an expiration time. Note that for writing / deleting cookies you need to use the HttpResponse object, and to read them you use the HttpRequest object. These are both accessible in your Controller class.

public void SetCookie(string key, string value, int expirationMinutes)
{
    var options = new CookieOptions();
    if (expirationMinutes > 0)
    {
        options.Expires = DateTime.Now.AddMinutes(expirationMinutes);
    }

    Response.Cookies.Append(key, value, options);
}

Then to read the cookie you need Request.Cookies[key] and to delete a cookie you need Response.Cookies.Delete(key) .

If you are storing sensitive information then you should use options.Secure = true to force HTTPS-only transmission of cookie data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM