Skip to content

Latest commit

 

History

History

Bet.Extensions.DataProtection

Bet.Extensions.DataProtection

GitHub license Build status NuGet Nuget feedz.io

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.

Summary

This library provides with ability to add DataProtection to any DotNetCore application.

buymeacoffee

Give a Star! ⭐

If you like or are using this project to learn or start your solution, please give it a star. Thanks!

Install

    dotnet add package Bet.Extensions.DataProtection

Azure Resources Setup

Azure Key Vault

Manage Key Vault in Azure Stack using PowerShell

  1. Create Azure Key In the Azure Key Vault

Azure Key Creation

  1. Access Policies -> Add Access Policy Azure Key Policy Creation

Azure Blob Storage

Make sure developer account and MSI account have Storage Blob Data Contributor Role assigned to the storage account.

Usage

  1. Add the following to Startup.cs ConfigureServices method:
    services.AddDataProtectionAzureStorage();
  1. 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>