Reputation: 1
How can ı Access my cookie value ?
I need to get cookie value UserID ı read cookie UserID but ı don't Access cookie in js and with model.
I am new mvc, I am using core identity.
I am tried to request.cookie in js , but its not correct way to get cookie
Upvotes: 0
Views: 3754
Reputation: 950
In ASP.Net Core, create cookies by js in following is not working (.Net Core can not get cookies).
<script>
document.cookie = "username=John Doe";
</script>
It need to use encodeURIComponent
to encoding.
<script>
var cookieValue = encodeURIComponent("John Doe");
document.cookie = "username=" + cookieValue;
</script>
and C# code :
public IActionResult ReadCookies()
{
//read cookie from Request object
string cookieValueFromReq = Request.Cookies["username"];
ViewBag.Message = cookieValueFromReq;
return View();
}
and it will get the cookies create by javascript.
Upvotes: 1