How to manage cookies in client machine in Asp.NET

In this asp.net tutorial, we will discuss how to manage cookies in client machine in asp.net using JavaScript.

Manage cookies in client machine in Asp.NET

Here I have a dialog box that is appearing in the page load. But when users click on the Acknowledge button, it will not appear next time till the end of the cookies expire date.

So here I have a simple HTML code to create a Dialog box that will appear in the client computer when visiting the site.

image 15
<body>
    <form id="form1" runat="server">
        <div id="dialog" title="Critial Alert">
            <p>Your computer might restart at any time and IT is not responsible in case of any data loss..</p>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal" style="background-color: forestgreen; color: white;" onclick="CheckAcknowledge();">Acknowledge </button>
                <button type="button" class="btn btn-default" data-dismiss="modal" style="background-color: red; color: white;" onclick="hide();">Remind me later </button>

            </div>
        </div>
    </form>
</body>
image 16

In the above screenshot, when users click on Acknowledge, It will hide from the page for a particular user. So I set the cookies in button click but before that, I am checking, if cookies already exist, then I hide the dialog, if it doesn’t exist then it will show to the user.

image 17
 $(function () {
        var AlertID = getCookie("notification-id");
        if (AlertID != "") {
            document.getElementById('dialog').style.display = "none";
        }
        else {
            $("#dialog").dialog();
        }

    });

Below is the code to get the cookies in asp.net.

image 20
function getCookie(cname) {
        var name = cname + "=";
        var decodedCookie = decodeURIComponent(document.cookie);
        var ca = decodedCookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') {
                c = c.substring(1);
            }
            if (c.indexOf(name) == 0) {
                return c.substring(name.length, c.length);
            }
        }
        return "";
    }

In above code, “notification-id” is my cookies name which i am storing in client browser.

Next when client click on Acknowledge button, I set the cookies in client browser with expire date.

image 18
function CheckAcknowledge() {

        setCookie("notification-id", "acknowledged", 30);
        $('#dialog').dialog('close');

    }

Below is the method to set the cookies in Asp.Net.

image 19
 function setCookie(cname, cvalue, exdays) {
        var d = new Date();
        d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
        var expires = "expires=" + d.toGMTString();
        document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
    }

Below is the code to modify cookies in Asp.Net.

image 21
function UpdateCookie() {
        var now = new Date();
        now.setMonth(now.getMonth() - 1);
        cookievalue = "1";

        document.cookie = "notification-id=" + cookievalue;
        document.cookie = "expires=" + now.toUTCString() + ";"
        document.write("Setting Cookies : " + "notification-id=" + cookievalue);
    }

When I click on the Acknowledge button, you can see the below screenshot where cookies is already set in the browser.

image 22

You can see, when I refresh the page next time, it is verifying the cookies, if it already exists then the dialog box will hide for the user.

image 23

When cookies is not exist in the client browser then, it will appear like below:

image 24
image 25

You may like following asp.net tutorials:

In this asp.net tutorial, we discussed how to manage cookies in client machine in asp.net using JavaScript. Specially, we checked how to set, get and modify cookies in asp.net.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>