歐盟一般資料保護規定 (GDPR) 支援 ASP.NET Core
ASP.NET Core提供 API 和範本,以協助符合部分歐盟一般資料保護規定 (GDPR) 需求:
- 專案範本包含擴充點和標記,您可以用隱私權取代並使用 cookie 原則。
- 頁面
Pages/Privacy.cshtml
或Views/Home/Privacy.cshtml
檢視提供頁面來詳細說明您的網站隱私權原則。
若要啟用預設 cookie 同意功能,例如在目前 ASP.NET Core範本產生的應用程式中,ASP.NET Core 2.2 範本中找到的同意功能:
將 新增
using Microsoft.AspNetCore.Http
至 using 指示詞的清單。將 新增 CookiePolicyOptions 至 和 UseCookiePolicy 至
Startup.ConfigureServices
Startup.Configure
:public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.Configure<CookiePolicyOptions>(options => { // This lambda determines whether user consent for non-essential // cookies is needed for a given request. options.CheckConsentNeeded = context => true; // requires using Microsoft.AspNetCore.Http; options.MinimumSameSitePolicy = SameSiteMode.None; }); services.AddRazorPages(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseCookiePolicy(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); }); } }
將 cookie 同意部分新增至
_Layout.cshtml
檔案:@*Previous markup removed for brevity*@ </header> <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"> © 2019 - RPCC - <a asp-area="" asp-page="/Privacy">Privacy</a> </div> </footer> <script src="~/lib/jquery/dist/jquery.js"></script> <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.js"></script> <script src="~/js/site.js" asp-append-version="true"></script> @RenderSection("Scripts", required: false) </body> </html>
將 _ Cookie ConsentPartial.cshtml 檔案新增至專案:
@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; }, false); })(); </script> }
選取本文的 ASP.NET Core 2.2 版,以閱讀同意 cookie 功能的相關資訊。
- 專案範本包含擴充點和標記,您可以用隱私權取代並使用 cookie 原則。
- cookie同意功能可讓您要求 (,並追蹤使用者) 同意以儲存個人資訊。 如果使用者尚未同意資料收集,且應用程式已 CheckConsentNeeded 設定
true
為 ,則不會將非基本 cookie 專案傳送至瀏覽器。 - Cookies 可以標示為基本。 即使使用者尚未同意且追蹤已停用,基本資訊 cookie 仍會傳送至瀏覽器。
- 停用追蹤時,TempData 和會話 cookie無法運作。
- 管理 Identity頁面提供下載和刪除使用者資料的連結。
範例應用程式可讓您測試新增至 ASP.NET Core 2.1 範本的大部分 GDPR 擴充點和 API。 如需測試指示,請參閱 讀我檔案 。
在範本產生的程式碼中 ASP.NET Core GDPR 支援
Razor 使用專案範本建立的頁面和 MVC 專案包括下列 GDPR 支援:
- CookiePolicyOptions 和 UseCookiePolicy 是在 類別中
Startup
設定。 - _ Cookie ConsentPartial.cshtmlpartial檢視。 此檔案中包含 [接受 ] 按鈕。 當使用者按一下 [ 接受 ] 按鈕時,會提供同意存放 cookie 區。
- 頁面
Pages/Privacy.cshtml
或Views/Home/Privacy.cshtml
檢視提供頁面來詳細說明您的網站隱私權原則。 _ Cookie ConsentPartial.cshtml檔案會產生頁面的連結 Privacy 。 - 對於使用個別使用者帳戶建立的應用程式,[管理] 頁面提供下載和刪除 個人資料的連結。
CookiePolicyOptions 和 UsePolicy Cookie
CookiePolicyOptions 在 中 Startup.ConfigureServices
初始化:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services
// to the container.
public void ConfigureServices(IServiceCollection services)
{
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;
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
// If the app uses session state, call AddSession.
// services.AddSession();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// This method gets called by the runtime. Use this method to configure the
// HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
// If the app uses session state, call Session Middleware after Cookie
// Policy Middleware and before MVC Middleware.
// app.UseSession();
app.UseMvc();
}
}
UseCookiePolicy 在 中 Startup.Configure
呼叫 :
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services
// to the container.
public void ConfigureServices(IServiceCollection services)
{
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;
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
// If the app uses session state, call AddSession.
// services.AddSession();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// This method gets called by the runtime. Use this method to configure the
// HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
// If the app uses session state, call Session Middleware after Cookie
// Policy Middleware and before MVC Middleware.
// app.UseSession();
app.UseMvc();
}
}
_ Cookie ConsentPartial.cshtml 部分檢視
_ Cookie ConsentPartial.cshtml部分檢視:
@using Microsoft.AspNetCore.Http.Features
@{
var consentFeature = Context.Features.Get<ITrackingConsentFeature>();
var showBanner = !consentFeature?.CanTrack ?? false;
var cookieString = consentFeature?.CreateConsentCookie();
}
@if (showBanner)
{
<nav id="cookieConsent" class="navbar navbar-default navbar-fixed-top" role="alert">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#cookieConsent .navbar-collapse">
<span class="sr-only">Toggle cookie consent banner</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<span class="navbar-brand"><span class="glyphicon glyphicon-info-sign" aria-hidden="true"></span></span>
</div>
<div class="collapse navbar-collapse">
<p class="navbar-text">
Use this space to summarize your privacy and cookie use policy.
</p>
<div class="navbar-right">
<a asp-page="/Privacy" class="btn btn-info navbar-btn">Learn More</a>
<button type="button" class="btn btn-default navbar-btn" data-cookie-string="@cookieString">Accept</button>
</div>
</div>
</div>
</nav>
<script>
(function () {
document.querySelector("#cookieConsent button[data-cookie-string]").addEventListener("click", function (el) {
document.cookie = el.target.dataset.cookieString;
document.querySelector("#cookieConsent").classList.add("hidden");
}, false);
})();
</script>
}
此部分:
- 取得使用者的追蹤狀態。 如果應用程式設定為需要同意,使用者必須先同意 cookie ,才能追蹤 。 如果需要同意, cookie 則同意面板會固定在 _Layout.cshtml 檔案所建立的導覽列頂端。
- 提供 HTML
<p>
元素來摘要您的隱私權和使用 cookie 原則。 - 提供頁面或檢視的連結, Privacy 您可以在其中詳細說明網站的隱私權原則。
基本 cookie s
如果尚未提供存放區的 cookie 同意,則只會 cookie 將標示為基本資訊傳送至瀏覽器。 下列程式碼是 cookie 不可或缺的:
public IActionResult OnPostCreateEssentialAsync()
{
HttpContext.Response.Cookies.Append(Constants.EssentialSec,
DateTime.Now.Second.ToString(),
new CookieOptions() { IsEssential = true });
ResponseCookies = Response.Headers[HeaderNames.SetCookie].ToString();
return RedirectToPage("./Index");
}
TempData 提供者和會話狀態 cookie 不是必要的
TempData 提供者cookie 並不重要。 如果追蹤已停用,TempData 提供者將無法運作。 若要在停用追蹤時啟用 TempData 提供者,請將 TempData cookie 標示為 中 Startup.ConfigureServices
的基本專案:
// The TempData provider cookie is not essential. Make it essential
// so TempData is functional when tracking is disabled.
services.Configure<CookieTempDataProviderOptions>(options => {
options.Cookie.IsEssential = true;
});
會話狀態cookies 不是必要的。 停用追蹤時,會話狀態無法運作。 下列程式碼讓會話 cookie 變得很重要:
services.AddSession(options =>
{
options.Cookie.IsEssential = true;
});
個人資料
ASP.NET Core使用個別使用者帳戶建立的應用程式,包括下載和刪除個人資料的程式碼。
選取使用者名稱,然後選取 [個人資料]:
注意:
- 若要產生程式
Account/Manage
代碼,請參閱Scaffold Identity。 - [刪除] 和 [下載] 連結只會對預設身分識別資料採取行動。 建立自訂使用者資料的應用程式必須擴充,才能刪除/下載自訂使用者資料。 如需詳細資訊,請參閱將自訂使用者資料新增、下載和刪除至 Identity。
- 由於外鍵而透過串聯刪除行為刪除使用者時,會刪除儲存在資料庫資料表
AspNetUserTokens
中的 Identity 使用者的已儲存權杖。 - 接受原則之前 cookie ,無法使用外部提供者驗證,例如 Facebook 和 Google。
待用加密
某些資料庫和儲存機制允許待用加密。 待用加密:
- 自動加密儲存的資料。
- 針對存取資料的軟體,加密沒有設定、程式設計或其他工作。
- 這是最簡單的安全選項。
- 允許資料庫管理金鑰和加密。
例如:
- Microsoft SQL和Azure SQL提供透明資料加密 (TDE) 。
- SQL Azure預設會加密資料庫
- Azure Blob、檔案、資料表和佇列儲存體預設會加密。
對於未提供待用內建加密的資料庫,您可以使用磁片加密來提供相同的保護。 例如:
- 適用于 Windows 伺服器的 BitLocker
- Linux: