public static void AddMyIdentity(this IServiceCollection services, IConfiguration config)
        {
            IdentityModelEventSource.ShowPII = true;

            var identityOptions = config.GetSection("identity").Get <NotifoIdentityOptions>() ?? new NotifoIdentityOptions();

            services.Configure <NotifoIdentityOptions>(config, "identity");

            services.AddIdentity <NotifoUser, NotifoRole>()
            .AddDefaultTokenProviders();

            services.AddSingletonAs <UserResolver>()
            .As <IUserResolver>();

            AddMyMongoDbIdentity(services);

            services.AddSingletonAs <UserCreator>()
            .AsSelf();

            services.AddIdentityServer()
            .AddAspNetIdentity <NotifoUser>()
            .AddClients()
            .AddIdentityResources()
            .AddApiResources();

            services.Configure <ApiAuthorizationOptions>(options =>
            {
                options.Clients.AddIdentityServerSPA("notifo", client => client
                                                     .WithLogoutRedirectUri("/authentication/logout-callback")
                                                     .WithRedirectUri("/authentication/login-callback")
                                                     .WithRedirectUri("/authentication/login-silent-callback.html"));
            });

            services.AddAuthorization();
            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = AlternativeSchema;
                options.DefaultChallengeScheme    = AlternativeSchema;
            })
            .AddPolicyScheme(AlternativeSchema, null, options =>
            {
                options.ForwardDefaultSelector = context =>
                {
                    if (ApiKeyHandler.IsApiKey(context.Request, out _))
                    {
                        return(ApiKeyDefaults.AuthenticationScheme);
                    }

                    return("IdentityServerJwt");
                };
            })
            .AddGoogle(identityOptions)
            .AddGithub(identityOptions)
            .AddApiKey()
            .AddIdentityServerJwt();

            services.TryAddEnumerable(ServiceDescriptor.Transient <IConfigureOptions <IdentityServerOptions>, IdentityOptions>());
        }
Esempio n. 2
0
        public static void AddMyIdentity(this IServiceCollection services, IConfiguration config)
        {
            IdentityModelEventSource.ShowPII = true;

            var identityOptions = config.GetSection("identity").Get <NotifoIdentityOptions>() ?? new NotifoIdentityOptions();

            services.Configure <NotifoIdentityOptions>(config, "identity");

            services.AddIdentity <IdentityUser, IdentityRole>()
            .AddDefaultTokenProviders();

            services.AddSingletonAs <UserCreator>()
            .AsSelf();

            services.AddSingletonAs <TokenStoreInitializer>()
            .AsSelf();

            services.AddSingletonAs <DefaultUserResolver>()
            .As <IUserResolver>();

            services.AddScopedAs <DefaultUserService>()
            .As <IUserService>();

            services.AddMyOpenIdDict();
            services.AddAuthorization();
            services.AddAuthentication()
            .AddPolicyScheme(Constants.IdentityServerOrApiKeyScheme, null, options =>
            {
                options.ForwardDefaultSelector = context =>
                {
                    if (ApiKeyHandler.IsApiKey(context.Request, out _))
                    {
                        return(ApiKeyDefaults.AuthenticationScheme);
                    }

                    return(OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme);
                };
            })
            .AddGoogle(identityOptions)
            .AddGithub(identityOptions)
            .AddApiKey();
        }
        public static void AddMyIdentity(this IServiceCollection services, IConfiguration config)
        {
            IdentityModelEventSource.ShowPII = true;

            var identityOptions = config.GetSection("identity").Get <NotifoIdentityOptions>() ?? new NotifoIdentityOptions();

            services.Configure <NotifoIdentityOptions>(config, "identity");

            services.AddIdentity <IdentityUser, IdentityRole>()
            .AddDefaultTokenProviders();

            services.AddSingletonAs <UserCreator>()
            .AsSelf();

            services.AddSingletonAs <DefaultUserResolver>()
            .As <IUserResolver>();

            services.AddScopedAs <DefaultUserService>()
            .As <IUserService>();

            services.AddIdentityServer(options =>
            {
                options.Authentication.CookieAuthenticationScheme = IdentityConstants.ApplicationScheme;

                options.Events.RaiseErrorEvents       = true;
                options.Events.RaiseInformationEvents = true;
                options.Events.RaiseFailureEvents     = true;
                options.Events.RaiseSuccessEvents     = true;

                options.UserInteraction.ErrorUrl = "/account/error";
            })
            .AddAspNetIdentity <IdentityUser>()
            .AddClients()
            .AddIdentityResources()
            .AddApiResources();

            services.Configure <IdentityServerOptions>((c, options) =>
            {
                var urlBuilder = c.GetRequiredService <IUrlGenerator>();

                options.IssuerUri = urlBuilder.BuildUrl();
            });

            services.Configure <ApiAuthorizationOptions>(options =>
            {
                options.Clients.AddIdentityServerSPA("notifo", client => client
                                                     .WithLogoutRedirectUri("/authentication/logout-callback")
                                                     .WithRedirectUri("/authentication/login-callback")
                                                     .WithRedirectUri("/authentication/login-silent-callback.html"));
            });

            services.AddAuthorization();
            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = Constants.IdentityServerOrApiKeyScheme;
                options.DefaultChallengeScheme    = Constants.IdentityServerOrApiKeyScheme;
            })
            .AddPolicyScheme(Constants.IdentityServerOrApiKeyScheme, null, options =>
            {
                options.ForwardDefaultSelector = context =>
                {
                    if (ApiKeyHandler.IsApiKey(context.Request, out _))
                    {
                        return(ApiKeyDefaults.AuthenticationScheme);
                    }

                    return(Constants.IdentityServerScheme);
                };
            })
            .AddGoogle(identityOptions)
            .AddGithub(identityOptions)
            .AddApiKey()
            .AddIdentityServerJwt();
        }