Examples - DMBServerHelper

Typed cookies, session definitions, localization lookups, and domain composition.

Cookies

Definitions hold storage metadata, lifetime, SameSite, Secure, and typed defaults.

Sessions

Session helpers expose typed values while keeping ASP.NET Core session access centralized.

Domains

DomainComposite parses hosts, ports, IPs, localhost, subdomains, and public suffixes.

Cookie Definitions

Theme cookie

Name LabsExampleTheme
Group Preference
Current value light
Generated data LabsExampleTheme=dark%20mode%3B%20beta%3Aon; expires=Wed, 23 Dec 2026 22:28:01 GMT; path=/; samesite=Lax; Secure

Consent cookie

Name LabsExampleConsent
Group Consent
Current value False
JavaScript var now = new Date(); var year = now.getFullYear(); var month = now.getMonth(); var day = now.getDate(); var next = new Date(year, month, day+ 365); document.cookie = 'LabsExampleConsent=' + encodeURIComponent('True') + '; SameSite=Lax; Path=/; expires=' + next.toUTCString() + '; Secure';

Cookie code

Cookie definition
CookieString themeCookie = new CookieString(
    "Labs Example Theme",
    "Theme preference",
    "Stores the selected visual theme.",
    CookieDefinitionGroup.Preference,
    "light",
    duration: 30,
    secure: true,
    limitSite: Microsoft.AspNetCore.Http.SameSiteMode.Lax);

string cookieData = themeCookie.GenerateCookieDataString("dark mode; beta:on");
string currentValue = themeCookie.GetValue(HttpContext);

Session Definitions

Profile session

Name LabsExampleProfile
Group Preference
Value Ada Lovelace

Visit counter

Name LabsExampleVisitCount
Group Navigation
Value 1

Session code

Session definition
SessionString profileSession = new SessionString(
    "Labs Example Profile",
    "Profile",
    "Stores a sample profile label.",
    SessionDefinitionGroup.Preference,
    "anonymous",
    deletable: true);

profileSession.SetValue(HttpContext, "Ada Lovelace");
string profile = profileSession.GetValue(HttpContext);

Localization Lookup

Registered key

Package data annotation localizer

FormBuilder_Field_Required
Required field

Labs host key

Host internal localizer

LABS_SAMPLE_KEY
This text comes from the labs host internal localization resources.

Fallback key

Internal localizer fallback

LABS_MISSING_SAMPLE_KEY
LABS_MISSING_SAMPLE_KEY

Localization code

Localization lookup
var labsLocalizerFactory = new ResourceManagerStringLocalizerFactory(
    Options.Create(new LocalizationOptions()),
    NullLoggerFactory.Instance);

WebLocalizer.InternalLocalizer.InjectResource(
    nameof(LabsInternalLocalization),
    labsLocalizerFactory.Create(typeof(LabsInternalLocalization)));

LocalizedString sample = WebLocalizer.GetInternal("LABS_SAMPLE_KEY");
LocalizedString message = WebLocalizer.GetDataAnnotation("FormBuilder_Field_Required");
LocalizedString fallback = WebLocalizer.GetInternal("LABS_MISSING_SAMPLE_KEY");

DomainComposite

The online Public Suffix List refresh is disabled here so the examples stay deterministic while rendering the documentation page.

Domain parsing matrix

Input Domain Subdomain Port Localhost HTTPS website
labs.idemobi.comidemobi.comlabsFalsehttps://labs.idemobi.com
https://docs.example.co.uk:8443/pathexample.co.ukdocs8443Falsehttps://docs.example.co.uk:8443
https://127.0.0.1:7174127.0.0.17174Truehttps://127.0.0.1:7174
https://[::1]:7174::17174Truehttps://[::1]:7174
localhost:7174localhost7174Truehttps://localhost:7174

Domain code

Domain parsing
DomainComposite composite = new DomainComposite(
    "https://docs.example.co.uk:8443/path",
    publicSuffixListOnlineRefreshEnabled: false,
    publicSuffixListRefreshTimeout: TimeSpan.FromSeconds(1));

string domain = composite.Domain;
string subdomain = composite.SubDomain;
string httpsWebsite = composite.HttpsWebsite;