.Net Authentication Flashcards
What components does authentication in .net consist of?
Authentication middleware
uses IAuthenticationService
uses Authentication handlers
The authentication service uses registered authentication handlers to complete authentication-related actions, e.g. Authenticate, Challenge, Forbid, SignIn, SignOut
What is authentication schema?
The registered authentication handler and its configuration options are called “schema”
How to specify the authentication scheme?
You can do it when registering authentication service.
And there are two ways:
- using specific schema-specific extensions, e.g. AddCookie, AddJwtBearer
- using AddScheme on AuthenticationBuilder
How to register authentication service?
By calling AddAuthentication extension method on IServiceCollection. It returns AuthenticationBuilder. One of the overloads allows specifying the default scheme, one allows configuring AuthenticationOptions itself.
How to use AddScheme on AuthenticationBuilder?
The method allows to create a schema with a particular name, options, handler, and configure options.
As you can see below, options are inheriter of AuthenticationSchemeOptions, handler is inheriter of AuthenticationHandler<TOptions></TOptions>
AuthenticationBuilder.AddScheme<TOptions,THandler> (string schemeName, Action<TOptions>? configureOptions) where TOptions : AuthenticationSchemeOptions, new() where THandler: AuthenticationHandler<TOptions>;</TOptions></TOptions>