Skip to content

Commit fae6b8e

Browse files
use cookie auth in variant service example
1 parent 4615186 commit fae6b8e

File tree

3 files changed

+21
-65
lines changed

3 files changed

+21
-65
lines changed

examples/VariantServiceDemo/HttpContextTargetingContextAccessor.cs

Lines changed: 0 additions & 53 deletions
This file was deleted.

examples/VariantServiceDemo/Pages/Index.cshtml.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using Microsoft.AspNetCore.Mvc;
1+
using Microsoft.AspNetCore.Authentication;
2+
using Microsoft.AspNetCore.Mvc;
23
using Microsoft.AspNetCore.Mvc.RazorPages;
34
using Microsoft.FeatureManagement;
5+
using System.Security.Claims;
46

57
namespace VariantServiceDemo.Pages
68
{
@@ -19,11 +21,22 @@ public IActionResult OnGet()
1921
{
2022
//
2123
// generate a new visitor
22-
string visitor = Random.Shared.Next().ToString();
24+
Username = Random.Shared.Next().ToString();
2325

24-
Response.Cookies.Append("username", visitor);
26+
// Clear Application Insights cookies and
27+
Response.Cookies.Delete("ai_user");
28+
Response.Cookies.Delete("ai_session");
2529

26-
Username = visitor;
30+
// Generate new user claim
31+
var claims = new List<Claim>
32+
{
33+
new Claim(ClaimTypes.Name, Username)
34+
};
35+
36+
var identity = new ClaimsIdentity(claims, "CookieAuth");
37+
var principal = new ClaimsPrincipal(identity);
38+
39+
HttpContext.SignInAsync("CookieAuth", principal);
2740

2841
return Page();
2942
}

examples/VariantServiceDemo/Program.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33
//
4-
using Microsoft.ApplicationInsights.Extensibility;
54
using Microsoft.FeatureManagement;
6-
using Microsoft.FeatureManagement.Telemetry.ApplicationInsights;
75
using VariantServiceDemo;
86

97
var builder = WebApplication.CreateBuilder(args);
@@ -14,14 +12,12 @@
1412
// Add services to the container.
1513
builder.Services.AddRazorPages();
1614

17-
builder.Services.AddHttpContextAccessor();
15+
//
16+
// Use cookie auth for simplicity and randomizing user
17+
builder.Services.AddAuthentication("CookieAuth");
1818

1919
builder.Services.AddApplicationInsightsTelemetry();
2020

21-
//
22-
// App Insights TargetingId Tagging
23-
builder.Services.AddSingleton<ITelemetryInitializer, TargetingTelemetryInitializer>();
24-
2521
//
2622
// Add variant implementations of ICalculator
2723
builder.Services.AddSingleton<ICalculator, DefaultCalculator>();
@@ -35,7 +31,7 @@
3531
// Including user targeting capability and the variant service provider of ICalculator which is bounded with the variant feature flag "Calculator"
3632
// Wire up evaluation event emission
3733
builder.Services.AddFeatureManagement()
38-
.WithTargeting<HttpContextTargetingContextAccessor>()
34+
.WithTargeting()
3935
.WithVariantService<ICalculator>("Calculator")
4036
.AddApplicationInsightsTelemetry();
4137

0 commit comments

Comments
 (0)