2023-03-07 17:16:47 +01:00
|
|
|
== How to fix it in ASP.NET
|
|
|
|
|
|
|
|
=== Code examples
|
2022-09-08 11:06:29 +02:00
|
|
|
|
|
|
|
include::../../common/fix/code-rationale.adoc[]
|
|
|
|
|
2022-10-18 16:03:10 +02:00
|
|
|
==== Noncompliant code example
|
2022-09-15 10:28:08 +02:00
|
|
|
|
|
|
|
[source,csharp,diff-id=1,diff-type=noncompliant]
|
2022-09-08 11:06:29 +02:00
|
|
|
----
|
|
|
|
using System.Web;
|
|
|
|
using System.Web.Mvc;
|
|
|
|
|
|
|
|
public class ExampleController : Controller
|
|
|
|
{
|
|
|
|
[HttpGet]
|
|
|
|
public IActionResult CheckCookie(string cookie)
|
|
|
|
{
|
|
|
|
if (Request.Cookies["ASP.NET_SessionId"] == null)
|
|
|
|
{
|
2022-09-15 10:28:08 +02:00
|
|
|
Response.Cookies.Append("ASP.NET_SessionId", cookie);
|
2022-09-08 11:06:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return View("Welcome");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
2022-09-15 10:28:08 +02:00
|
|
|
|
|
|
|
==== Compliant solution
|
|
|
|
|
|
|
|
[source,csharp,diff-id=1,diff-type=compliant]
|
2022-09-08 11:06:29 +02:00
|
|
|
----
|
|
|
|
using System.Web;
|
|
|
|
using System.Web.Mvc;
|
|
|
|
|
|
|
|
public class ExampleController : Controller
|
|
|
|
{
|
|
|
|
[HttpGet]
|
|
|
|
public IActionResult CheckCookie()
|
|
|
|
{
|
|
|
|
if (Request.Cookies["ASP.NET_SessionId"] == null)
|
|
|
|
{
|
|
|
|
return View("GetCookie");
|
|
|
|
}
|
|
|
|
|
|
|
|
return View("Welcome");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
2022-09-15 10:28:08 +02:00
|
|
|
include::../../common/fix/how-does-this-work.adoc[]
|