Exemple #1
0
 public static string GetIPAddress()
 {
     try
     {
         var _accessor = EngineerContext.GetService <IHttpContextAccessor>();
         return(_accessor.HttpContext.Connection.RemoteIpAddress.ToString());
     }
     catch
     {
         return(null);
     }
 }
Exemple #2
0
        public static T GetCache <T>(string key, int timeInMinute, Func <T> result)
        {
            var _cache = EngineerContext.GetService <IMemoryCache>();
            T   cacheEntry;

            if (!_cache.TryGetValue(key, out cacheEntry))
            {
                // Key not in cache, so get data.
                cacheEntry = result();

                // Set cache options.
                var cacheEntryOptions = new MemoryCacheEntryOptions()
                                        // Keep in cache for this time, reset time if accessed.
                                        .SetSlidingExpiration(TimeSpan.FromMinutes(timeInMinute));

                // Save data in cache.
                _cache.Set(key, cacheEntry, cacheEntryOptions);
            }
            return(cacheEntry);
        }
Exemple #3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext <StudyContext>(o =>
                                                 o.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddScoped(typeof(IRepository <>), typeof(Repository <>));
            services.AddRepository("Blog.Infra.Data", "Repository");
            services.AddServices("Blog.Application", "Service");
            services.AddScoped <IUnitOfWork, UnitOfWork>();
            services.AddMediatR(typeof(Startup));
            services.AddScoped <IMediatorHandler, InMemoryBus>();
            //services.AddTransient(typeof(IUserTokenAppService), typeof(UserTokenAppService));
            services.Configure <JwtSettings>(Configuration.GetSection(nameof(JwtSettings)));

            services.AddScoped <IRequestHandler <RegisterStudentCommand, Unit>, StudentCommandHandler>();

            services.AddScoped <INotificationHandler <DomainNotification>, DomainNotificationHandler>();

            //添加jwt验证:
            services.AddAuthentication(option =>
            {
                option.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                option.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(o =>
            {
                o.SecurityTokenValidators.Clear();
                o.SecurityTokenValidators.Add(new JwtValidator());
            });

            services.AddMvc()
            .AddJsonOptions(o => { o.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; })
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            services.AddAutoMapperSetup();
            services.AddSwagger();


            ///仓储引擎注入
            EngineerContext.Initialize(new GeneralEngineer(services.BuildServiceProvider()));
        }
Exemple #4
0
        public EngineerController(EngineerContext context)
        {
            _context = context;

            if (_context.ListOfEngineers.Count() == 0)
            {
                _context.ListOfEngineers.Add(new Engineer {
                    Name = "Jenny Baker", ShiftCount = 0
                });
                _context.ListOfEngineers.Add(new Engineer {
                    Name = "Finn Davis", ShiftCount = 0
                });
                _context.ListOfEngineers.Add(new Engineer {
                    Name = "Alex Goodman", ShiftCount = 0
                });
                _context.ListOfEngineers.Add(new Engineer {
                    Name = "Trever Deely", ShiftCount = 0
                });
                _context.ListOfEngineers.Add(new Engineer {
                    Name = "John Blogs", ShiftCount = 0
                });
                _context.ListOfEngineers.Add(new Engineer {
                    Name = "Mary Monroe", ShiftCount = 0
                });
                _context.ListOfEngineers.Add(new Engineer {
                    Name = "Sarah Silver", ShiftCount = 0
                });
                _context.ListOfEngineers.Add(new Engineer {
                    Name = "Fred Pear", ShiftCount = 0
                });
                _context.ListOfEngineers.Add(new Engineer {
                    Name = "Pete Samson", ShiftCount = 0
                });
                _context.ListOfEngineers.Add(new Engineer {
                    Name = "Michael Moore", ShiftCount = 0
                });
                _context.SaveChanges();
            }
        }
Exemple #5
0
        public static void ClearCached(string key)
        {
            var _cache = EngineerContext.GetService <IMemoryCache>();

            _cache.Remove(key);
        }
Exemple #6
0
 public FilesController(EngineerContext context, IOptions <AppSettings> appSettings)
 {
     this.context     = context;
     this.appSettings = appSettings.Value;
 }
Exemple #7
0
 public UsersController(EngineerContext context, IOptions <AppSettings> appSettings, MessageManager messageManager)
 {
     this.context        = context;
     this.appSettings    = appSettings.Value;
     this.messageManager = messageManager;
 }
Exemple #8
0
 public MessagesController(EngineerContext context)
 {
     this.context = context;
 }
Exemple #9
0
 public CommentsController(EngineerContext context)
 {
     this.context = context;
 }
Exemple #10
0
 static HttpHelper()
 {
     _httpContextAccessor = EngineerContext.GetService <IHttpContextAccessor>();
 }
Exemple #11
0
 public static void AddStaticHelper(this IServiceCollection services)
 {
     DependencyInjectionHelper.Init(services);
     EngineerContext.Init(services.BuildServiceProvider());
 }
Exemple #12
0
 public PostsController(EngineerContext context)
 {
     this.context = context;
 }
Exemple #13
0
 public GroupsController(EngineerContext context)
 {
     this.context = context;
 }
Exemple #14
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            ApiHelper.Init(Configuration);
            WebHelper.Init(ApiHelper.GetServer());
            JwtTokenHelper.Init(Configuration);

            //services.AddMvc(options => {
            //    options.Filters.Add(typeof(ExceptionFilter));
            //    options.OutputFormatters.Insert(0, new CustomOutputFormater());
            //}).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            services.AddDbContext <AQConfigContext>(options =>
                                                    options.UseSqlServer(ApiHelper.GetConnectionStringConfig()));

            services.AddDbContext <AQYachtContext>(options =>
                                                   options.UseSqlServer(ApiHelper.GetConnectionStringYacht()));

            services.AddDbContext <AQDiningContext>(options =>
                                                    options.UseSqlServer(ApiHelper.GetConnectionStringDining()));

            services.AddDbContext <AQEvisaContext>(options =>
                                                   options.UseSqlServer(ApiHelper.GetConnectionStringEvisa()));

            services.AddDbContext <AQHotelContext>(options =>
                                                   options.UseSqlServer(ApiHelper.GetConnectionStringHotel()));

            services.AddDbContext <AQCMSContext>(options =>
                                                 options.UseSqlServer(ApiHelper.GetConnectionStringCMS()));

            ServiceHelper.Init(services.BuildServiceProvider());

            services.AddSwaggerGen(c =>
            {
                //.Netcore 2.2

                /*c.SwaggerDoc("v1", new OpenApiInfo { Title = "Admin Api", Version = "v1" });
                 *
                 * c.AddSecurityDefinition(JwtBearerDefaults.AuthenticationScheme, new ApiKeyScheme
                 * {
                 *  Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
                 *  Name = "Authorization",
                 *  In = "header",
                 *  Type = "apiKey"
                 * });
                 * c.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>>
                 * {
                 * { JwtBearerDefaults.AuthenticationScheme, new string[] { } },
                 * });
                 *
                 * var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                 * var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                 * c.IncludeXmlComments(xmlPath);
                 * c.DescribeAllEnumsAsStrings();*/


                //Netcore 3.1
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "Admin API", Version = "v1"
                });
                c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Name         = "Authorization",
                    Type         = SecuritySchemeType.ApiKey,
                    Scheme       = "Bearer",
                    BearerFormat = "JWT",
                    In           = ParameterLocation.Header,
                    Description  = "JWT Authorization header using the Bearer scheme."
                });

                c.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id   = "Bearer"
                            }
                        },
                        new string[] {}
                    }
                });
            });
            services.AddAuthentication()
            .AddJwtBearer(options =>
            {
                options.TokenValidationParameters = JwtTokenHelper.GetValidationParameters();
                options.SaveToken            = true;
                options.RequireHttpsMetadata = false;
            });

            // Init & register Automapper
            services.AddAutoMapper(typeof(AutoMapperConfig));
            services.AddHttpContextAccessor();
            RegisterDependencyInjection(services);
            EngineerContext.Init(services.BuildServiceProvider());
        }