-1

We have one requirement where we have to capture the website cookies. We can capture the other cookies except localstorage cookies in httpclient response.

But how can we read the localstorage cookies at server side. As localstorage are resides at client side.

So is there any way where we can load the website at server side and read those cookies?

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • What you mean about localstorage cookies? Do you mean you want to get the clent-side's local storage? If this is your requirement, you should write codes in the client side to send the localstroage value to the server side, The server side couldn't directly read the localstorage value. – Brando Zhang Sep 18 '20 at 12:51
  • Yes, want to get the client side local storage cookies. And I agree with you that we have to write code in the server side. Do you have any reference link or sample code for the same? I've tried with HTMLUnit but didn't get success into it. – Anil Prajapati Sep 20 '20 at 09:08

1 Answers1

0

If you want to know how to get the localstorage and post it to asp.net core backend.

I suggest you could try to use jquery to achieve it ,you could use localStorage.setItem(key,value); to set localStorage.getItem(key); to get, then you could use ajax to post request to backend.

More details, you could refer to below codes:

Client-side set and get localstorage by using ajax:

<input name="SetValue" id="SetValue" value="SetValue" type="button" />

<br />
<input name="SendValue" id="SendValue" value="SendValue" type="button" />


@section scripts{
    <script>
        $(function () {
            $("#SetValue").click(function () {
                localStorage.setItem("test", "value")
            });

            $("#SendValue").click(function () {
 
                var str1 = localStorage.getItem("test");     
                $.ajax({
                    type: "POST",
                    data: JSON.stringify(str1),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    url: '/home/Myaction',
                    success: function (data) {
                    

                    },
                    error: function (xhr) {

                        console.log(xhr.responseText);
                        alert("Error has occurred..");
                    }
                });
               
            });
        });
    </script>
}

MVC controller:

    [HttpPost]
    public ActionResult MyAction([FromBody]string str1)
    {
        return View();
        // code here 
    }

Result:

enter image description here

Brando Zhang
  • 22,586
  • 6
  • 37
  • 65