In ASP.NET Core 2.0 the .UseAuthentication() middleware has a breaking change that no longer allows the old syntax mentioned here to work.
The new version appears to
The example you posted doesn't seem to be a real code anyways (i.e. new CookieAuthenticationOptions() being inside the AddAuthentication call, rather than as argument to AddCookieAuthentication). You don't add authorizations inside the AddAuthorization call, you just setup standards middlewares here, see this announcement.
Old:
services.AddAuthentication(sharedOptions =>
sharedOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme);
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
AutomaticChallenge = true,
AutomaticAuthenticate = true,
New:
app.AddAuthentication(o => {
o.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
o.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
o.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
});
And the
services.AddXxxAuthentication(new XxxOptions() { ... });
are replaced with
services.AddXxxAuthentication(options => {
});
to be inline with all other methods which accept a configuration.
Also always worth a look at the ASP.NET Core Announcements GitHub Repository, where the ASP.NET Core Team announces breaking changes for the next version, just select a specific milestone there, i.e. 2.0.0-preview1, 2.0.0-preview2, etc.
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);