Is there any way to read a cookie created by JavaScript code in the Controller of ASP.NET Core?

yogihosting

I want to ask that is there any way to read a cookie created by JavaScript code in the Controller?

My application is in ASP.NET Core.

In my View I am creating the cookie by JS like this:

<script type="text/javascript">
  document.cookie = "username=John Doe";
</script>

Now in my action method of the controller I try to read the cookie but it did not find any cookie there:

public IActionResult Index()
{
    var boh = Request.Cookies["username"];
    return View();
}

The value of boh comes out to be null saying no cookie.

I dug a bit deeper and inspected the "Network" area and found that cookie is not added to Response. See the below image:

enter image description here

If I can add the cookie to the response then I think it will be accessible in the controller, but how??

Is there any solution???

Thanks & Regards

yogihosting

I found out that you need to encode the cookie value as pointed by out some expert. So I corrected it by doing the encoding:

$(function () {
  var cookieValue = encodeURIComponent("John Doe");
  document.cookie = "username=" + cookieValue;
});

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Asp Net core Controller URL parameters

分類Dev

Read status code on response from asp.net core in angular 6

分類Dev

ASP.NET MVC: Is Controller created for every request?

分類Dev

It there any way to automatically visualizate JavaScript code tree?

分類Dev

SQL distributed cache created on Asp.net core startup

分類Dev

.Net Core cookie will not be set

分類Dev

AJAX paramter to ASP .NET Core 2 Controller Action

分類Dev

ASP.NET Core 2.0: Authenticate a route without a controller

分類Dev

ASP.NET Core constant requests to home controller

分類Dev

ASP.NET Core Pass Enumerable of objects to Get Action on Controller

分類Dev

ASP.NET Core .css Extension Goes To Controller

分類Dev

Handle Claims while testing controller in asp.net core

分類Dev

Read solution data files ASP.Net Core

分類Dev

Read solution data files ASP.Net Core

分類Dev

Read solution data files ASP.Net Core

分類Dev

cookie .net Core3とAsp.netを共有する

分類Dev

Performant Way to Check for Token Expiration in ASP.Net Core

分類Dev

Calling ASP.NET MVC 4 Controller from Javascript

分類Dev

Problem scaffolding new controller using Entity Framework Core in ASP.NET Core 2.1 MVC

分類Dev

ASP.NET Core return JSON with status code

分類Dev

Run async code during startup in a ASP.Net Core application

分類Dev

Cookie ASP.NET Core1.0の値を置き換えます

分類Dev

Asp.Net WebApi Core 2.0IDとJWTBearerのCookieなし

分類Dev

ASP.NET Core2.0の偽造防止Cookie

分類Dev

ASP.Net Core 2.1 API JWT Cookieなしのセッション?

分類Dev

How to decrypt .AspNetCore.Identity.Application cookie in ASP.NET Core 3.0?

分類Dev

ASP.NET Core 1.0-MVC6-Cookieの有効期限

分類Dev

Cookie asp.net core へのアクセス方法

分類Dev

Is there any way to programmatically generate a CouchDB cookie?

Related 関連記事

  1. 1

    Asp Net core Controller URL parameters

  2. 2

    Read status code on response from asp.net core in angular 6

  3. 3

    ASP.NET MVC: Is Controller created for every request?

  4. 4

    It there any way to automatically visualizate JavaScript code tree?

  5. 5

    SQL distributed cache created on Asp.net core startup

  6. 6

    .Net Core cookie will not be set

  7. 7

    AJAX paramter to ASP .NET Core 2 Controller Action

  8. 8

    ASP.NET Core 2.0: Authenticate a route without a controller

  9. 9

    ASP.NET Core constant requests to home controller

  10. 10

    ASP.NET Core Pass Enumerable of objects to Get Action on Controller

  11. 11

    ASP.NET Core .css Extension Goes To Controller

  12. 12

    Handle Claims while testing controller in asp.net core

  13. 13

    Read solution data files ASP.Net Core

  14. 14

    Read solution data files ASP.Net Core

  15. 15

    Read solution data files ASP.Net Core

  16. 16

    cookie .net Core3とAsp.netを共有する

  17. 17

    Performant Way to Check for Token Expiration in ASP.Net Core

  18. 18

    Calling ASP.NET MVC 4 Controller from Javascript

  19. 19

    Problem scaffolding new controller using Entity Framework Core in ASP.NET Core 2.1 MVC

  20. 20

    ASP.NET Core return JSON with status code

  21. 21

    Run async code during startup in a ASP.Net Core application

  22. 22

    Cookie ASP.NET Core1.0の値を置き換えます

  23. 23

    Asp.Net WebApi Core 2.0IDとJWTBearerのCookieなし

  24. 24

    ASP.NET Core2.0の偽造防止Cookie

  25. 25

    ASP.Net Core 2.1 API JWT Cookieなしのセッション?

  26. 26

    How to decrypt .AspNetCore.Identity.Application cookie in ASP.NET Core 3.0?

  27. 27

    ASP.NET Core 1.0-MVC6-Cookieの有効期限

  28. 28

    Cookie asp.net core へのアクセス方法

  29. 29

    Is there any way to programmatically generate a CouchDB cookie?

ホットタグ

アーカイブ