Örnek projeyi github üzerinden indirmek veya görüntülemek için https://github.com/ayzdru/AspNetCookieConsent adresine gidebilirsiniz.
Kişisel Verileri Koruma Kurumu (KVKK) Nedir?
Kişisel Verileri Koruma Kurumu, Türkiye'de kişisel verilerin korunmasını sağlamak ve gözetmek için kurulmuş olan düzenleyici ve denetleyici bir kurumdur.
Avrupa Birliği Genel Veri Koruma Yönetmeliği (GDPR) Nedir?
Genel Veri Koruma Yönetmeliği 2016/679, Avrupa Birliği hukukunda, tüm Avrupa Birliği ve Avrupa Ekonomik Alanı içerisinde yer alan bireyler için veri koruma ve gizliliğine ilişkin bir yönetmeliktir.
Çerez Kullanım Onayı için Kodlamaya Başlayalım
Startup.cs dosyası
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
private static bool DisallowsSameSiteNone(string userAgent)
{
if (String.IsNullOrWhiteSpace(userAgent))
return false;
if (userAgent.Contains("CPU iPhone OS 12") ||
userAgent.Contains("iPad; CPU OS 12"))
{
return true;
}
if (userAgent.Contains("Macintosh; Intel Mac OS X 10_14") &&
userAgent.Contains("Version/") && userAgent.Contains("Safari"))
{
return true;
}
if (userAgent.Contains("Chrome/5") || userAgent.Contains("Chrome/6"))
{
return true;
}
return false;
}
private static void CheckSameSite(HttpContext httpContext, CookieOptions options)
{
if (options.SameSite == SameSiteMode.None)
{
var userAgent = httpContext.Request.Headers["User-Agent"].ToString();
if (DisallowsSameSiteNone(userAgent))
{
options.SameSite = SameSiteMode.Unspecified;
}
}
}
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
options.OnAppendCookie = cookieContext =>
CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
options.OnDeleteCookie = cookieContext =>
CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
});
services.AddRazorPages();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseCookiePolicy();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
_CookieConsentPartial.cshtml dosyası
@using Microsoft.AspNetCore.Http.Features
@{
var consentFeature = Context.Features.Get<ITrackingConsentFeature>();
var showBanner = !consentFeature?.CanTrack ?? false;
var cookieString = consentFeature?.CreateConsentCookie();
}
@if (showBanner)
{
<div id="cookieConsent" class="alert alert-info alert-dismissible fade show" role="alert">
Use this space to summarize your privacy and cookie use policy. <a asp-page="/Privacy">Learn More</a>.
<button type="button" class="accept-policy close" data-dismiss="alert" aria-label="Close" data-cookie-string="@cookieString">
<span aria-hidden="true">Accept</span>
</button>
</div>
<script>
(function () {
var button = document.querySelector("#cookieConsent button[data-cookie-string]");
button.addEventListener("click", function (event) {
document.cookie = button.dataset.cookieString;
document.getElementById('CookieConsentRevokeForm').style.display = 'inline';
}, false);
})();
</script>
}
_CookieConsentRevokePartial.cshtml dosyası
@using Microsoft.AspNetCore.Http.Features
@{
bool showRevoke = false;
var consentFeature = Context.Features.Get<ITrackingConsentFeature>();
if (consentFeature != null && consentFeature.IsConsentNeeded)
{
showRevoke = consentFeature.CanTrack;
}
string display;
}
@if (showRevoke)
{
display = "inline";
}
else
{
display = "none";
}
<form id="CookieConsentRevokeForm" style="display:@display" asp-page="/Privacy" asp-page-handler="Withdraw" method="post">
<button type="submit" class="btn btn-link">Revoke Cookie Consent</button>
</form>
_Layout.cshtml dosyasında gerekli yerlere Partiallarımızı ekliyoruz.
....
<div class="container">
<partial name="_CookieConsentPartial" />
<main role="main" class="pb-3">
@RenderBody()
</main>
</div>
....
<footer class="border-top footer text-muted">
<div class="container">
© 2020 - AspNetCookieConsent - <a asp-area="" asp-page="/Privacy">Privacy</a> <partial name="_CookieConsentRevokePartial" />
</div>
</footer>
Görüşmek dileğiyle, hoşcakalın..