The second letter in the Hebrew alphabet is the ב bet/beit. Its meaning is "house". In the ancient pictographic Hebrew it was a symbol resembling a tent on a landscape.
Note: Pre-release packages are distributed via feedz.io.
This library provides with ability to add DataProtection
to any DotNetCore application.
If you like or are using this project to learn or start your solution, please give it a star. Thanks!
dotnet add package Bet.Extensions.DataProtection
Manage Key Vault in Azure Stack using PowerShell
- Create Azure Key In the Azure Key Vault
Make sure developer account and MSI account have Storage Blob Data Contributor
Role assigned to the storage account.
- Add the following to
Startup.cs
ConfigureServices
method:
services.AddDataProtectionAzureStorage();
- Add options to
appsettings.json
:
"DataProtectionAzureStorage": {
"KeyVaultKeyId": "https://{name}.vault.azure.net/keys/{keyname}/{keyId}", // valut
"ConnectionString": "",
"Token": "",
"Name": "",
"ContainerName": "dataprotection",
"KeyBlobName": "some-keys.xml"
},
- Razor Page Usage
public class IndexModel : PageModel
{
private const string CookieName = "TestCookie";
private readonly IDataProtector _dataProtector;
public IndexModel(IDataProtectionProvider dataProtectionProvider)
{
_dataProtector = dataProtectionProvider.CreateProtector("Test");
}
public string CookieValue { get; set; }
public bool ShowCookieValue => !string.IsNullOrEmpty(CookieValue);
public void OnGet()
{
if (!Request.Cookies.TryGetValue(CookieName, out var cookieValue))
{
var valueToSetInCookie = $"Some text set in cookie at {DateTime.Now.ToString()}";
var encryptedValue = _dataProtector.Protect(valueToSetInCookie);
Response.Cookies.Append(CookieName, encryptedValue, new Microsoft.AspNetCore.Http.CookieOptions
{
IsEssential = true
});
return;
}
CookieValue = _dataProtector.Unprotect(cookieValue);
}
}
Page View:
@if (Model.ShowCookieValue) {
<h2>Decrypted value from cookie:</h2>
<p>@Model.CookieValue</p>
} else {
<p><strong>No Test Cookie exists:</strong> refresh browser.</p>
}
- MVC Controller
public class HomeController : Controller
{
private const string CookieName = "TestCookie";
private readonly IDataProtector _dataProtector;
public HomeController(IDataProtectionProvider dataProtectionProvider)
{
_dataProtector = dataProtectionProvider.CreateProtector("Test");
}
public IActionResult Index()
{
if (!Request.Cookies.TryGetValue(CookieName, out var cookieValue))
{
string valueToSetInCookie = $"Some text set in cookie at {DateTime.Now.ToString()}";
var encryptedValue = _dataProtector.Protect(valueToSetInCookie);
Response.Cookies.Append(CookieName, encryptedValue, new Microsoft.AspNetCore.Http.CookieOptions
{
IsEssential = true
});
return RedirectToAction("Index");
}
ViewBag.CookieValue = _dataProtector.Unprotect(cookieValue);
return View();
}
}
@{
ViewData["Title"] = "Home Page";
}
<h2>Decrypted value from cookie:</h2>
<p>@ViewBag.CookieValue</p>