Skip to content

AddAuthCredentials ​

Registers IAuthUserService<TUser>, IAuthSessionService<TUser>, IAuthPasswordService, IAuthEmailService, and IAuthTwoFactorService<TUser> as scoped services, alongside an AuthDbContext<TUser> resolving to the same TDbContext the request already holds, so credential data lives in the application's own database; every credential write then opens a transaction of its own on that context and throws InvalidOperationException when the application already has one open.

Call it after AddAuth. Credential flows read AuthSettings, resolve the current application through IAppHostResolver, and mint access tokens through the JWT package's generator, so the same issuer, secret, lifetime, and audience rules apply to both.

Usage ​

csharp
using AlmightyShogun.AspNet.Core;
using AlmightyShogun.AspNet.Auth;
using Microsoft.EntityFrameworkCore;
using AlmightyShogun.AspNet.Localization;
using AlmightyShogun.AspNet.Auth.Credentials;
using AlmightyShogun.AspNet.RequestValidation;

builder.Services
    .AddMessageLocalization(builder.Configuration)
    .AddHttpErrorResponseWriter()
    .AddExceptionHandling()
    .AddAuth(builder.Configuration)
    .AddAspNetValidation()
    .AddDbContext<AppDbContext>(options => ...)
    .AddAuthCredentials<AppDbContext, AppUser>(builder.Configuration);

Parameters

configuration: IConfiguration
The application configuration the optional AuthCredentials section is bound from. Pass builder.Configuration.

registerExceptionHandler: bool
Whether to add the handler that turns the package's exceptions into standardized HTTP errors. Pass false to answer them with a handler of your own; the mapper stays registered either way.
Default: true

Returns

The IServiceCollection instance with the credential authentication services registered.

Type signature ​

csharp
public IServiceCollection AddAuthCredentials<TDbContext, TUser>(
    IConfiguration configuration,
    bool registerExceptionHandler = true
) where TDbContext : AuthDbContext<TUser> where TUser : AuthUser;

All packages are released under the MIT License.