Exemple #1
0
        private static IJointBuilder AddJwt(this IJointBuilder builder, JwtOptions options,
                                            Action <JwtBearerOptions> optionsFactory = null)
        {
            if (!builder.TryRegister(RegistryName))
            {
                return(builder);
            }

            builder.Services.AddTransient <IJwtHandler, JwtHandler>();
            builder.Services.AddSingleton <IAccessTokenService, InMemoryAccessTokenService>();
            builder.Services.AddTransient <AccessTokenValidatorMiddleware>();

            var tokenValidationParameters = TokenValidationFactory.CreateParameters(options);

            tokenValidationParameters.AddIssuerSigningKey(options);
            tokenValidationParameters.AddAuthenticationType(options);
            tokenValidationParameters.AddNameClaimType(options);
            tokenValidationParameters.AddRoleClaimType(options);

            builder.Services
            .AddAuthentication(o =>
            {
                o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                o.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, option =>
            {
                option.Authority                  = options.Authority;
                option.Audience                   = options.ValidAudience;
                option.MetadataAddress            = options.MetadataAddress;
                option.SaveToken                  = options.SaveToken;
                option.RefreshOnIssuerKeyNotFound = options.RefreshOnIssuerKeyNotFound;
                option.RequireHttpsMetadata       = options.RequireHttpsMetadata;
                option.IncludeErrorDetails        = options.IncludeErrorDetails;
                option.TokenValidationParameters  = tokenValidationParameters;
                if (!string.IsNullOrWhiteSpace(options.Challenge))
                {
                    option.Challenge = options.Challenge;
                }

                optionsFactory?.Invoke(option);
            });

            builder.Services.AddSingleton(options);
            builder.Services.AddSingleton(tokenValidationParameters);

            return(builder);
        }
Exemple #2
0
    private static IServiceCollection AddJwt(this IServiceCollection service, JwtOptions options,
                                             Action <JwtBearerOptions> optionsFactory = null)
    {
        service.AddTransient <IJwtHandler, JwtHandler>()
        .AddScoped <AccessTokenValidatorMiddleware>()
        .AddScoped <IPasswordService, PasswordService>()
        .AddScoped <IRng, Rng>()
        .AddSingleton <IPasswordHasher <IPasswordService>, PasswordHasher <IPasswordService> >()
        .AddSingleton <IAccessTokenService, InMemoryAccessTokenService>()
        .AddSingleton <IHttpContextAccessor, HttpContextAccessor>();

        var tokenValidationParameters = TokenValidationFactory.CreateParameters(options);

        tokenValidationParameters.AddIssuerSigningKey(options);
        tokenValidationParameters.AddAuthenticationType(options);
        tokenValidationParameters.AddNameClaimType(options);
        tokenValidationParameters.AddRoleClaimType(options);

        service.AddAuthentication(o =>
        {
            o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            o.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
        })
        .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, option =>
        {
            option.Authority                  = options.Authority;
            option.Audience                   = options.ValidAudience;
            option.MetadataAddress            = options.MetadataAddress;
            option.SaveToken                  = options.SaveToken;
            option.RefreshOnIssuerKeyNotFound = options.RefreshOnIssuerKeyNotFound;
            option.RequireHttpsMetadata       = options.RequireHttpsMetadata;
            option.IncludeErrorDetails        = options.IncludeErrorDetails;
            option.TokenValidationParameters  = tokenValidationParameters;
            if (!string.IsNullOrWhiteSpace(options.Challenge))
            {
                option.Challenge = options.Challenge;
            }

            optionsFactory?.Invoke(option);
        });

        service.AddSingleton(options);
        service.AddSingleton(tokenValidationParameters);

        return(service);
    }