Exemple #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            Registrar.Register(services, Configuration);

            services.AddIdentity <User, Role>()
            .AddDefaultTokenProviders();

            services.Configure <IdentityOptions>(options => { options.User.RequireUniqueEmail = true; });
            if (WebHostEnvironment.IsDevelopment())
            {
                services.Configure <IdentityOptions>(options =>
                {
                    options.Password.RequireDigit           = false;
                    options.Password.RequireLowercase       = false;
                    options.Password.RequireUppercase       = false;
                    options.Password.RequireNonAlphanumeric = false;
                    options.Password.RequiredLength         = 4;
                    options.Password.RequiredUniqueChars    = 1;

                    options.User.RequireUniqueEmail = true;
                });
            }

            services.Configure <CookieAuthenticationOptions>(
                IdentityConstants.ApplicationScheme,
                options =>
            {
                options.LoginPath  = "/login";
                options.LogoutPath = "/logout";
            });

            var clientApplications = ClientApplication.GetClientApplications(Configuration);
            var builder            = services.AddIdentityServer(options =>
            {
                options.Events.RaiseErrorEvents       = true;
                options.Events.RaiseInformationEvents = true;
                options.Events.RaiseFailureEvents     = true;
                options.Events.RaiseSuccessEvents     = true;
                options.UserInteraction.ErrorUrl      = "/error";
                options.UserInteraction.LogoutUrl     = "/logout";
                options.UserInteraction.LoginUrl      = "/login";
                options.UserInteraction.LogoutUrl     = "/logout";
            })
                                     .AddInMemoryIdentityResources(new List <IdentityResource>
            {
                new IdentityResources.OpenId(),
                new IdentityResources.Profile(),
                new IdentityResources.Email()
            })
                                     .AddInMemoryClients(clientApplications.Select(x => x.BuildIdentityServerClient()).ToList())
                                     .AddAspNetIdentity <User>();

            if (WebHostEnvironment.IsDevelopment())
            {
                builder.AddDeveloperSigningCredential();
            }
            else
            {
                throw new Exception("need to configure key material");
            }

            services.AddControllersWithViews(options =>
            {
                // add the "feature" convention
                options.Conventions.Add(new FeatureConvention());
                // Auto add [Area("areaName"] to controllers.
                options.Conventions.Add(new AutoAreaConvention());
            })
            .AddRazorOptions(options =>
            {
                // using the "feature" convention, expand the paths
                options.ViewLocationExpanders.Add(new FeatureViewLocationExpander());
            })
            .AddFluentValidation()
            .AddRazorRuntimeCompilation();
            services.AddAuthentication();

            services.Configure <RouteOptions>(options => { options.LowercaseUrls = true; });

            // Add all of our validators
            foreach (var validator in ValidatorDiscovery.DiscoverValidators(typeof(Startup).Assembly))
            {
                services.AddTransient(validator.Interface, validator.Implementation);
            }
        }