Updated as this has changed slightly again in the 2.0 RTM bits
It turns out it's a lot easier than expected, but as the official documentation hasn't been updated yet, here is exactly what works for plain Cookie auth:
Configuration:
In ConfigureServices()
configure the specific Authentication mechanism:
services
.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(o =>
{
o.LoginPath = "/api/login";
o.LogoutPath = "/api/logout";
// additional config options here
});
Then in Configure()
to actually hook up the middleware:
app.UseAuthentication();
Using the Auth Components
Then to use the actual Auth components the logic has shifted from the HttpContext.Authentication
object, down to just HttpContext
in application logic like controller code:
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(identity));
or:
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…