© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
17 replies
sonodan.

✅ Authentication with Cookies

I'm playing around with cookies to get a better understanding. My code:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAuthentication("cookie")
    .AddCookie("local");

var app = builder.Build();

app.UseAuthentication();

app.MapGet("/", () => "Hello World!");

app.MapGet("/login", async (HttpContext ctx) =>
{
    var claims = new List<Claim>();
    claims.Add(new Claim("usr", "daniel"));
    var identity = new ClaimsIdentity(claims, "local");
    var user = new ClaimsPrincipal(identity);
    await ctx.SignInAsync("local", user);
});

app.MapGet("/user-info", (HttpContext ctx) =>
{
    return ctx.User.FindFirstValue("usr") ?? "empty";
});

app.Run();
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAuthentication("cookie")
    .AddCookie("local");

var app = builder.Build();

app.UseAuthentication();

app.MapGet("/", () => "Hello World!");

app.MapGet("/login", async (HttpContext ctx) =>
{
    var claims = new List<Claim>();
    claims.Add(new Claim("usr", "daniel"));
    var identity = new ClaimsIdentity(claims, "local");
    var user = new ClaimsPrincipal(identity);
    await ctx.SignInAsync("local", user);
});

app.MapGet("/user-info", (HttpContext ctx) =>
{
    return ctx.User.FindFirstValue("usr") ?? "empty";
});

app.Run();


Currently, the
user-info
user-info
endpoint returns "empty" after getting a cookie from the
login
login
endpoint. When I changed to
AddCookie("cookie")
AddCookie("cookie")
, and change the ClaimsIdentity and Signin to "cookie", it returns a claim value. I was under the impression I could name the
AddCookie
AddCookie
scheme whatever I would like. Could someone please explain?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Cookies and JWT for Authentication
C#CC# / help
11mo ago
❔ Authentication with owinCookie
C#CC# / help
4y ago
OpenApi Login endpoint with cookies
C#CC# / help
2mo ago
❔ Massive issue with Authentication
C#CC# / help
3y ago