I will post a reply in the simplest form that I know, and that can be used in other languages.
The logic is simple. You create a cookie that will be expired per session, so every time you close the browser it will be deleted. And at your _Layout.cshtml
you check if the cookie exists. If it exists it does nothing, but if it does not exist you will make a request and save whatever you want.
So, let's go to the codes.
First, let's understand a bit more about working with cookies in this question here .
This is done, let's go to our file _Layout.cshtml
(or the layout file that is used on all pages) and use the following code:
<script>
//Função para ler o cookie
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
$(document).ready(function () {
//Buscando o cookie
var NewAccess = readCookie('NewAccess');
//Verificando se o cookie existe
if (!NewAccess) {
document.cookie = "NewAccess=true; expires=0;";
$.post('@Url.Action("SalvarLogin","Home")')//Coloque a URL para salvar os dados
}
});
</script>
As the question is in Asp.Net MVC
, just do this in Controller:
[HttpPost]
public JsonResult SalvarLogin()
{
//Método para salvar aqui
return null;//Retorno que dessejar
}
In this way you will always perform the check you want.
I should note that, of course, if a person clears cookies, he will consider it as a new access.