Пример #1
0
        public override int Run(string[] remainingArguments)
        {
            try
            {
                var env  = new ContextService(InstanceContext.DeployedOrLocal);
                var uow  = new UnitOfWork(_conf["Databases:IdentityEntities_EFCore"], env);
                var data = new DefaultDataFactory(uow);

                if (_create)
                {
                    Console.WriteLine();
                    Console.WriteLine("\tPress key to create default data...");
                    Console.ReadKey();

                    data.CreateSettings();
                    data.CreateIssuers();
                    data.CreateAudiences();
                    data.CreateAudienceRoles();
                    data.CreateRoles();
                    data.CreateLogins();
                    data.CreateUsers();
                    data.CreateUserLogins();
                    data.CreateUserRoles();

                    Console.WriteLine("\tCompleted create default data...");
                    Console.WriteLine();
                }
                else if (_destroy)
                {
                    Console.WriteLine();
                    Console.WriteLine("\tPress key to destroy default data...");
                    Console.ReadKey();

                    data.Destroy();

                    Console.WriteLine("\tCompleted destroy default data...");
                    Console.WriteLine();
                }
                else if (_destroyAll)
                {
                    Console.WriteLine();
                    Console.WriteLine("\tPress key to destroy all data...");
                    Console.ReadKey();

                    data.Destroy();

                    Console.WriteLine("\tCompleted destroy all data...");
                    Console.WriteLine();
                }

                return(StandardOutput.FondFarewell());
            }
            catch (Exception ex)
            {
                return(StandardOutput.AngryFarewell(ex));
            }
        }
Пример #2
0
        protected override void ConfigureWebHost(IWebHostBuilder builder)
        {
            var conf = (IConfiguration) new ConfigurationBuilder()
                       .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                       .Build();

            var env = new ContextService(InstanceContext.SystemTest);
            var map = new MapperConfiguration(x => x.AddProfile <AutoMapperProfile_EFCore>())
                      .CreateMapper();

            builder.ConfigureServices(sc =>
            {
                sc.AddSingleton <IConfiguration>(conf);
                sc.AddSingleton <IContextService>(env);
                sc.AddSingleton <IMapper>(map);
                sc.AddSingleton <IAuthorizationHandler, IdentityUsersAuthorize>();
                sc.AddSingleton <IAuthorizationHandler, IdentityServicesAuthorize>();
                sc.AddScoped <IUnitOfWork, UnitOfWork>(_ =>
                {
                    var uow = new UnitOfWork(conf["Databases:IdentityEntities_EFCore"], env);

                    var data = new DefaultDataFactory(uow);
                    data.CreateSettings();
                    data.CreateUserLogins();
                    data.CreateUserRoles();

                    return(uow);
                });
                sc.AddSingleton <IOAuth2JwtFactory, OAuth2JwtFactory>();

                /*
                 * do not use dependency injection for unit of work below. is used
                 * only for owin authentication configuration.
                 */

                var owin = new UnitOfWork(conf["Databases:IdentityEntities_EFCore"], env);

                var data = new DefaultDataFactory(owin);
                data.CreateIssuers();
                data.CreateAudiences();

                var issuers = owin.Issuers.Get()
                              .Select(x => x.Name + ":" + conf["IdentityTenant:Salt"]);

                var issuerKeys = owin.Issuers.Get()
                                 .Select(x => x.IssuerKey);

                var audiences = owin.Audiences.Get()
                                .Select(x => x.Name);

                sc.AddControllers()
                .AddNewtonsoftJson(opt =>
                {
                    opt.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                })
                //https://github.com/aspnet/Mvc/issues/5992
                .AddApplicationPart(typeof(BaseController).Assembly);
                sc.AddCors();
                sc.AddAuthentication(opt =>
                {
                    opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                    opt.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
                    opt.DefaultForbidScheme       = JwtBearerDefaults.AuthenticationScheme;
                    opt.DefaultScheme             = JwtBearerDefaults.AuthenticationScheme;
                    opt.DefaultSignOutScheme      = JwtBearerDefaults.AuthenticationScheme;
                    opt.DefaultSignInScheme       = JwtBearerDefaults.AuthenticationScheme;
                }).AddJwtBearer(jwt =>
                {
                    jwt.IncludeErrorDetails       = true;
                    jwt.TokenValidationParameters = new TokenValidationParameters
                    {
                        ValidIssuers             = issuers.ToArray(),
                        IssuerSigningKeys        = issuerKeys.Select(x => new SymmetricSecurityKey(Encoding.Unicode.GetBytes(x))).ToArray(),
                        ValidAudiences           = audiences.ToArray(),
                        AudienceValidator        = AudiencesValidator.Multiple,
                        ValidateIssuer           = true,
                        ValidateAudience         = true,
                        ValidateIssuerSigningKey = true,
                        ValidateLifetime         = true,
                        RequireAudience          = true,
                        RequireExpirationTime    = true,
                        RequireSignedTokens      = true,
                    };
                });
                sc.AddAuthorization(opt =>
                {
                    opt.AddPolicy(DefaultConstants.OAuth2ROPGrants, humans =>
                    {
                        humans.Requirements.Add(new IdentityUsersAuthorizeRequirement());
                    });
                    opt.AddPolicy(DefaultConstants.OAuth2CCGrants, servers =>
                    {
                        servers.Requirements.Add(new IdentityServicesAuthorizeRequirement());
                    });
                });
                sc.AddSwaggerGen(opt =>
                {
                    opt.SwaggerDoc("v1", new OpenApiInfo {
                        Title = "Reference", Version = "v1"
                    });
                });
                sc.Configure <ForwardedHeadersOptions>(opt =>
                {
                    opt.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
                });
            });

            builder.Configure(app =>
            {
                app.UseForwardedHeaders();
                app.UseStaticFiles();
                app.UseSwagger(opt =>
                {
                    opt.RouteTemplate = "help/{documentName}/index.json";
                });
                app.UseSwaggerUI(opt =>
                {
                    opt.RoutePrefix = "help";
                    opt.SwaggerEndpoint("v1/index.json", "Reference");
                });
                app.UseRouting();
                app.UseCors(opt => opt
                            .AllowAnyOrigin()
                            .AllowAnyHeader()
                            .AllowAnyMethod());
                app.UseAuthentication();
                app.UseAuthorization();
                app.UseEndpoints(opt =>
                {
                    opt.MapControllers();
                });
            });
        }