.Net Core cookie will not be set

Persyl

In .Net Core MVC project: I'm trying to set a simple cookie in the easiest way in my controller-action but can not get it to be persistent and show up in the browser.

My code:

public IActionResult IndexPost()
{
    var option = new CookieOptions();
    option.Expires = DateTime.Now.AddMinutes(60);
    Response.Cookies.Append(cookieName, "SomeCookieValue", option);
    return View();
}

But in the browser (Chrome) I can't see it or even read it with:

var cookieValue = Request.Cookies[cookieName];

(cookieName is a variable set with name of the cookie)

If using Chrome extension "EditThisCookie" I can set it manually to ensure that Request.Cookies[cookieName] actually works, so error is in the Append-cookie of my code somehow.

Sigge

Starting from ASP.NET Core 2.1, the templates include a GDPR compliant configuration of your CookiePolicyOptions in Startup.cs, namely:

services.Configure<CookiePolicyOptions>(options =>
{
    // This lambda determines whether user consent for non-essential cookies is needed for a given request.
    options.CheckConsentNeeded = context => true;
    options.MinimumSameSitePolicy = SameSiteMode.None;
});

The CheckConsentNeeded option of true will prevent any non-essential cookies from being sent to the browser (no Set-Cookie header) without the user's explicit permission.

You can either change this behaviour, or mark your cookie as essential by setting the IsEssential property to true when creating it:

var options = new CookieOptions
{
    Expires = DateTime.Now.AddMinutes(60),
    IsEssential = true
};

Response.Cookies.Append("rudeCookie", "I don't need no user to tell me it's ok.", options);

Read more here: https://docs.microsoft.com/en-us/aspnet/core/security/gdpr?view=aspnetcore-2.1

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

.NET Core 2.0CookieイベントOnRedirectToLogin

分類Dev

Use cookie, authorize attribute, create session for application in .net core 2.1

分類Dev

.Net Core 2.2Cookie認証の問題

分類Dev

Asp.net Core 1.1.2 OpenID接続Cookieがリダイレクト時に作成されず、Set-Cookieヘッダーが追加されます

分類Dev

Set current working directory on exit (.NET Core)

分類Dev

SQLCipher .NET with Entity Core - set or remove password

分類Dev

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

分類Dev

CORS応答からのset-cookieヘッダーを無視するブラウザー(asp.net core 2.0 web api)

分類Dev

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

分類Dev

.NET Core 2.0Cookie認証-リダイレクトしない

分類Dev

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

分類Dev

.NET CORE API with Angular 4 - The cookie token and the request token were swapped

分類Dev

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

分類Dev

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

分類Dev

.NET Core 3Cookie認証がIDを設定しない

分類Dev

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

分類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

Python Mechanize set cookie

分類Dev

How to set up Automapper in ASP.NET Core

分類Dev

VSTS/TFS set environment variable ASP.NET core

分類Dev

How to set default datetime format for .net core 2.0 webapi

分類Dev

Set Cookie or localStorage for Cookie Banner with JavaScript

分類Dev

ASP.Net Coreの「Cookie認証」はそのCookieへの参照をどこに保存しますか?

分類Dev

ASP .NET Core - How to tell if data contract property was not set or set to null

分類Dev

ASP.NET Coreの_Layout.cshtmlのCookieにアクセスする

分類Dev

ASP.NET Core 2.0でCookieを保存および復元する方法は?

分類Dev

ASP.NET Core 3.0で.AspNetCore.Identity.Application Cookieを復号化する方法は?

Related 関連記事

  1. 1

    .NET Core 2.0CookieイベントOnRedirectToLogin

  2. 2

    Use cookie, authorize attribute, create session for application in .net core 2.1

  3. 3

    .Net Core 2.2Cookie認証の問題

  4. 4

    Asp.net Core 1.1.2 OpenID接続Cookieがリダイレクト時に作成されず、Set-Cookieヘッダーが追加されます

  5. 5

    Set current working directory on exit (.NET Core)

  6. 6

    SQLCipher .NET with Entity Core - set or remove password

  7. 7

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

  8. 8

    CORS応答からのset-cookieヘッダーを無視するブラウザー(asp.net core 2.0 web api)

  9. 9

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

  10. 10

    .NET Core 2.0Cookie認証-リダイレクトしない

  11. 11

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

  12. 12

    .NET CORE API with Angular 4 - The cookie token and the request token were swapped

  13. 13

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

  14. 14

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

  15. 15

    .NET Core 3Cookie認証がIDを設定しない

  16. 16

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

  17. 17

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

  18. 18

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

  19. 19

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

  20. 20

    Python Mechanize set cookie

  21. 21

    How to set up Automapper in ASP.NET Core

  22. 22

    VSTS/TFS set environment variable ASP.NET core

  23. 23

    How to set default datetime format for .net core 2.0 webapi

  24. 24

    Set Cookie or localStorage for Cookie Banner with JavaScript

  25. 25

    ASP.Net Coreの「Cookie認証」はそのCookieへの参照をどこに保存しますか?

  26. 26

    ASP .NET Core - How to tell if data contract property was not set or set to null

  27. 27

    ASP.NET Coreの_Layout.cshtmlのCookieにアクセスする

  28. 28

    ASP.NET Core 2.0でCookieを保存および復元する方法は?

  29. 29

    ASP.NET Core 3.0で.AspNetCore.Identity.Application Cookieを復号化する方法は?

ホットタグ

アーカイブ