diff --git a/Microsoft.FeatureManagement.sln b/Microsoft.FeatureManagement.sln index 3b8c8661..65481890 100644 --- a/Microsoft.FeatureManagement.sln +++ b/Microsoft.FeatureManagement.sln @@ -25,7 +25,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TargetingConsoleApp", "exam EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.FeatureManagement.Telemetry.ApplicationInsights", "src\Microsoft.FeatureManagement.Telemetry.ApplicationInsights\Microsoft.FeatureManagement.Telemetry.ApplicationInsights.csproj", "{7964DC66-B2D3-412D-B18A-86D1E07D149D}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EvaluationDataToApplicationInsights", "examples\EvaluationDataToApplicationInsights\EvaluationDataToApplicationInsights.csproj", "{1502529E-47E9-4306-98C4-BF6CF7C7C275}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VariantAndTelemetryDemo", "examples\VariantAndTelemetryDemo\VariantAndTelemetryDemo.csproj", "{1502529E-47E9-4306-98C4-BF6CF7C7C275}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorServerApp", "examples\BlazorServerApp\BlazorServerApp.csproj", "{12BAB5A6-4EEB-4917-B5D9-4AFB6253008E}" EndProject diff --git a/README.md b/README.md index 5c9718c4..22acae9f 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ Feature management provides a way to develop and expose application functionalit * [ASP.NET Core Web App (Razor Page)](./examples/RazorPages) * [ASP.NET Core Web App (MVC)](./examples/FeatureFlagDemo) * [Blazor Server App](./examples/BlazorServerApp) -* [ASP.NET Core Web App with Feature Flag Telemetry](./examples/EvaluationDataToApplicationInsights) +* [ASP.NET Core Web App with Variants and Telemetry](./examples/VariantAndTelemetryDemo) * [ASP.NET Core Web App with Variant Service](./examples/VariantServiceDemo) ## Contributing diff --git a/examples/EvaluationDataToApplicationInsights/HttpContextTargetingContextAccessor.cs b/examples/EvaluationDataToApplicationInsights/HttpContextTargetingContextAccessor.cs deleted file mode 100644 index eeaa16b1..00000000 --- a/examples/EvaluationDataToApplicationInsights/HttpContextTargetingContextAccessor.cs +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT license. -// -using Microsoft.FeatureManagement.FeatureFilters; - -namespace EvaluationDataToApplicationInsights -{ - /// - /// Provides an implementation of that creates a targeting context using info from the current HTTP request. - /// - public class HttpContextTargetingContextAccessor : ITargetingContextAccessor - { - private const string TargetingContextLookup = "HttpContextTargetingContextAccessor.TargetingContext"; - private readonly IHttpContextAccessor _httpContextAccessor; - - public HttpContextTargetingContextAccessor(IHttpContextAccessor httpContextAccessor) - { - _httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor)); - } - - public ValueTask GetContextAsync() - { - HttpContext httpContext = _httpContextAccessor.HttpContext; - - // - // Try cache lookup - if (httpContext.Items.TryGetValue(TargetingContextLookup, out object value)) - { - return new ValueTask((TargetingContext)value); - } - - // - // Grab username from cookie - string username = httpContext.Request.Cookies["username"]; - - var groups = new List(); - - // - // Build targeting context based on user info - var targetingContext = new TargetingContext - { - UserId = username, - Groups = groups - }; - - // - // Cache for subsequent lookup - httpContext.Items[TargetingContextLookup] = targetingContext; - - return new ValueTask(targetingContext); - } - } -} diff --git a/examples/EvaluationDataToApplicationInsights/Pages/RandomizeUser.cshtml b/examples/EvaluationDataToApplicationInsights/Pages/RandomizeUser.cshtml deleted file mode 100644 index fa29a988..00000000 --- a/examples/EvaluationDataToApplicationInsights/Pages/RandomizeUser.cshtml +++ /dev/null @@ -1,4 +0,0 @@ -@page -@model EvaluationDataToApplicationInsights.Pages.RandomizeUserModel -@{ -} diff --git a/examples/EvaluationDataToApplicationInsights/Pages/RandomizeUser.cshtml.cs b/examples/EvaluationDataToApplicationInsights/Pages/RandomizeUser.cshtml.cs deleted file mode 100644 index dad3744d..00000000 --- a/examples/EvaluationDataToApplicationInsights/Pages/RandomizeUser.cshtml.cs +++ /dev/null @@ -1,18 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.RazorPages; - -namespace EvaluationDataToApplicationInsights.Pages -{ - public class RandomizeUserModel : PageModel - { - public IActionResult OnGet() - { - // Clear Application Insights cookies and generate new username - Response.Cookies.Delete("ai_user"); - Response.Cookies.Delete("ai_session"); - Response.Cookies.Append("username", Random.Shared.Next().ToString()); - - return RedirectToPage("/Index"); - } - } -} diff --git a/examples/EvaluationDataToApplicationInsights/Pages/Checkout.cshtml b/examples/VariantAndTelemetryDemo/Pages/Checkout.cshtml similarity index 100% rename from examples/EvaluationDataToApplicationInsights/Pages/Checkout.cshtml rename to examples/VariantAndTelemetryDemo/Pages/Checkout.cshtml diff --git a/examples/EvaluationDataToApplicationInsights/Pages/Checkout.cshtml.cs b/examples/VariantAndTelemetryDemo/Pages/Checkout.cshtml.cs similarity index 67% rename from examples/EvaluationDataToApplicationInsights/Pages/Checkout.cshtml.cs rename to examples/VariantAndTelemetryDemo/Pages/Checkout.cshtml.cs index ceaaef99..ea8a9104 100644 --- a/examples/EvaluationDataToApplicationInsights/Pages/Checkout.cshtml.cs +++ b/examples/VariantAndTelemetryDemo/Pages/Checkout.cshtml.cs @@ -1,6 +1,6 @@ using Microsoft.AspNetCore.Mvc.RazorPages; -namespace EvaluationDataToApplicationInsights.Pages +namespace VariantAndTelemetryDemo.Pages { public class CheckoutModel : PageModel { diff --git a/examples/EvaluationDataToApplicationInsights/Pages/Error.cshtml b/examples/VariantAndTelemetryDemo/Pages/Error.cshtml similarity index 100% rename from examples/EvaluationDataToApplicationInsights/Pages/Error.cshtml rename to examples/VariantAndTelemetryDemo/Pages/Error.cshtml diff --git a/examples/EvaluationDataToApplicationInsights/Pages/Error.cshtml.cs b/examples/VariantAndTelemetryDemo/Pages/Error.cshtml.cs similarity index 92% rename from examples/EvaluationDataToApplicationInsights/Pages/Error.cshtml.cs rename to examples/VariantAndTelemetryDemo/Pages/Error.cshtml.cs index f20de344..4aba79bc 100644 --- a/examples/EvaluationDataToApplicationInsights/Pages/Error.cshtml.cs +++ b/examples/VariantAndTelemetryDemo/Pages/Error.cshtml.cs @@ -2,7 +2,7 @@ using Microsoft.AspNetCore.Mvc.RazorPages; using System.Diagnostics; -namespace EvaluationDataToApplicationInsights.Pages +namespace VariantAndTelemetryDemo.Pages { [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] [IgnoreAntiforgeryToken] diff --git a/examples/EvaluationDataToApplicationInsights/Pages/Index.cshtml b/examples/VariantAndTelemetryDemo/Pages/Index.cshtml similarity index 100% rename from examples/EvaluationDataToApplicationInsights/Pages/Index.cshtml rename to examples/VariantAndTelemetryDemo/Pages/Index.cshtml diff --git a/examples/EvaluationDataToApplicationInsights/Pages/Index.cshtml.cs b/examples/VariantAndTelemetryDemo/Pages/Index.cshtml.cs similarity index 94% rename from examples/EvaluationDataToApplicationInsights/Pages/Index.cshtml.cs rename to examples/VariantAndTelemetryDemo/Pages/Index.cshtml.cs index 0fbcf0b7..07b1d300 100644 --- a/examples/EvaluationDataToApplicationInsights/Pages/Index.cshtml.cs +++ b/examples/VariantAndTelemetryDemo/Pages/Index.cshtml.cs @@ -3,7 +3,7 @@ using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.FeatureManagement; -namespace EvaluationDataToApplicationInsights.Pages +namespace VariantAndTelemetryDemo.Pages { public class IndexModel : PageModel { @@ -22,7 +22,7 @@ public IndexModel( public async Task OnGet() { - Username = Request.Cookies["username"]; + Username = HttpContext.User.Identity.Name; if (string.IsNullOrEmpty(Username)) { diff --git a/examples/VariantAndTelemetryDemo/Pages/RandomizeUser.cshtml b/examples/VariantAndTelemetryDemo/Pages/RandomizeUser.cshtml new file mode 100644 index 00000000..8aa505b1 --- /dev/null +++ b/examples/VariantAndTelemetryDemo/Pages/RandomizeUser.cshtml @@ -0,0 +1,4 @@ +@page +@model VariantAndTelemetryDemo.Pages.RandomizeUserModel +@{ +} diff --git a/examples/VariantAndTelemetryDemo/Pages/RandomizeUser.cshtml.cs b/examples/VariantAndTelemetryDemo/Pages/RandomizeUser.cshtml.cs new file mode 100644 index 00000000..781339d1 --- /dev/null +++ b/examples/VariantAndTelemetryDemo/Pages/RandomizeUser.cshtml.cs @@ -0,0 +1,30 @@ +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.RazorPages; +using System.Security.Claims; + +namespace VariantAndTelemetryDemo.Pages +{ + public class RandomizeUserModel : PageModel + { + public IActionResult OnGet() + { + // Clear Application Insights cookies and + Response.Cookies.Delete("ai_user"); + Response.Cookies.Delete("ai_session"); + + // Generate new user claim + var claims = new List + { + new Claim(ClaimTypes.Name, Random.Shared.Next().ToString()) + }; + + var identity = new ClaimsIdentity(claims, "CookieAuth"); + var principal = new ClaimsPrincipal(identity); + + HttpContext.SignInAsync("CookieAuth", principal); + + return RedirectToPage("/Index"); + } + } +} diff --git a/examples/EvaluationDataToApplicationInsights/Pages/Shared/_Layout.cshtml b/examples/VariantAndTelemetryDemo/Pages/Shared/_Layout.cshtml similarity index 85% rename from examples/EvaluationDataToApplicationInsights/Pages/Shared/_Layout.cshtml rename to examples/VariantAndTelemetryDemo/Pages/Shared/_Layout.cshtml index c8d3f799..365b135c 100644 --- a/examples/EvaluationDataToApplicationInsights/Pages/Shared/_Layout.cshtml +++ b/examples/VariantAndTelemetryDemo/Pages/Shared/_Layout.cshtml @@ -6,10 +6,10 @@ - @ViewData["Title"] - EvaluationDataToApplicationInsights + @ViewData["Title"] - VariantAndTelemetryDemo - + @Html.Raw(JavaScriptSnippet.FullScript) @@ -20,7 +20,7 @@