Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between AddAuthentication and AddAuthenticationCore?

Looking at the code for AuthenticationServiceCollectionExtensions.AddAuthentication() vs AuthenticationCoreServiceCollectionExtensions.AddAuthenticationCore(), it looks like AddAuthentication implicitly calls AddAuthenticationCore, adds some other goodies, and then returns a new instance of AuthenticationBuilder instead of just returning IServiceCollection.

Am I understanding the code correctly? If so, are there generally any reasons to call AddAuthenticationCore instead of AddAuthentication outside of writing your own extension?

like image 313
Jerreck Avatar asked Feb 03 '20 20:02

Jerreck


1 Answers

Actually there is a reason. Currently AddAuthentication() also adds data protection services, which you may not need - for example if you are writing your own Authentication Scheme. So instead you can do this:

services.AddAuthenticationCore(o => {
    o.DefaultScheme = "My Custom Scheme";
});
services.AddWebEncoders();
services.AddSingleton<ISystemClock, SystemClock>();
var authBuilder = new AuthenticationBuilder(services);

however I fully expect this to break in future versions of asp.net core as it's undocumented and a bit of a hack.

like image 115
Rocklan Avatar answered Oct 17 '22 20:10

Rocklan