// This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); //注册实体映射 ColumnMapper.SetMapper(); return(AutofacRegister.RegisterAutofac(services)); }
public void ConfigureServices(IServiceCollection services) { //调用前面的静态方法,将Dapper的实体类和数据表的映射关系注册 ColumnMapper.SetMapper(); services.AddControllers(); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.Configure <CookiePolicyOptions>(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.None; }); services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(options => { options.LoginPath = new PathString("/Admin/Login"); options.ExpireTimeSpan = TimeSpan.FromDays(3); }); //Configuration.GetSection(""); services.AddSingleton <IUserRepository, UserRepository>(r => new UserRepository("cqsheep_admin_website", "server=192.168.150.130;database={0};userid=root;password=root1234")); services.AddSingleton <IAdminUserService, AdminUserService>(); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); ColumnMapper.SetMapper <UserEntity>(); }
public DatabaseConfigRepository(string dbPath) : base(dbPath) { ColumnMapper.SetMapper <DatabaseConfigEntity>(); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { //services.AddGeek(opt => opt.UseIdGen(1)) // .AddDb<MySqlConnection>(Configuration.GetConnectionString("DefaultConnection")); services.AddGeek() .AddDb <MySqlConnection>(Configuration.GetConnectionString("DefaultConnection")); //添加Swagger services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "GeekTeach Api Doc", Version = "v1.0" }); // 获取xml文件名 var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; // 获取xml文件路径 var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); // 添加控制器层注释,true表示显示控制器注释 c.IncludeXmlComments(xmlPath, true); #region 启用swagger验证功能 // Add security definitions c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme() { Description = "Please enter into field the word 'Bearer' followed by a space and the JWT value", Name = "Authorization", In = ParameterLocation.Header, Type = SecuritySchemeType.ApiKey, }); c.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference() { Id = "Bearer", Type = ReferenceType.SecurityScheme } }, Array.Empty <string>() } }); #endregion }); services.AddCors(options => { options.AddPolicy("any", builder => { builder.AllowAnyOrigin()//允许所有地址访问 .AllowAnyMethod() .AllowAnyHeader(); //.WithOrigins("")//指定接受访问的地址 //.AllowCredentials()//指定处理cookie 使用AllowAnyOrigin时不可以使用这个 }); }); services.AddControllers().AddNewtonsoftJson(options => { options.SerializerSettings.ContractResolver = new DefaultContractResolver(); options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; }); services.AddOptions(); #region JWT //services.Configure<JwtOptions>(Configuration.GetSection(nameof(JwtOptions))); services.AddJwtBearer(Configuration); #endregion //调用前面的静态方法,将映射关系注册 ColumnMapper.SetMapper(); }
/// <summary> /// 配置 /// </summary> /// <param name="services"></param> public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); //获取IP services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); #region swagger和JWT //添加swagger services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "通用框架2019", Version = "V1 ", Description = "Sparkle831143", Contact = new Contact { Name = "Sparkle:", Url = "/views/test.html", Email = "*****@*****.**" } }); // 添加JWT(Bearer 输入框) var security = new Dictionary <string, IEnumerable <string> > { { "Sparkle_Framework2019", new string[] { } }, }; // 校验方案 c.AddSecurityRequirement(security); c.AddSecurityDefinition("Sparkle_Framework2019", new ApiKeyScheme { Description = "JWT授权(数据将在请求头中进行传输) 直接在下框中输入Bearer {token}(注意两者之间是一个空格)\"", Name = "Authorization", //jwt默认的参数名称 In = "header", //jwt默认存放Authorization信息的位置(请求头中) Type = "apiKey" }); //添加Swagger xml说明文档 var basepath = Path.GetDirectoryName(typeof(Program).Assembly.Location); var xmlpath = Path.Combine(basepath, "Sparkle_Framework2019.xml"); c.IncludeXmlComments(xmlpath); }); #region JWT //认证 services.AddAuthentication(x => { x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(o => { o.TokenValidationParameters = new TokenValidationParameters { // 是否开启签名认证 ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(Token.SecurityKey)), // 发行人验证,这里要和token类中Claim类型的发行人保持一致 ValidateIssuer = true, ValidIssuer = "Sparkle",//发行人 // 接收人验证 ValidateAudience = true, ValidAudience = "User",//订阅人 ValidateLifetime = true, ClockSkew = TimeSpan.Zero, }; }); #endregion #endregion #region 跨域 //添加跨域访问 services.AddCors(options => { options.AddPolicy("Cors", builder => builder.AllowAnyOrigin() //添加跨域访问规则 .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials()); }); #endregion #region 实体映射 //实体映射 ColumnMapper.SetMapper(); #endregion #region 获取token //获取请求头 services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>(); #endregion #region 依赖注入 var IOCbuilder = new ContainerBuilder();//建立容器 List <Assembly> programlist = new List <Assembly> { Assembly.Load("Common"), Assembly.Load("DAL") }; //批量反射程序集 foreach (var q in programlist) { IOCbuilder.RegisterAssemblyTypes(q).AsImplementedInterfaces(); //注册容器 } IOCbuilder.Populate(services); //将service注入到容器 var ApplicationContainner = IOCbuilder.Build(); //登记创建容器 return(new AutofacServiceProvider(ApplicationContainner)); //IOC接管 #endregion }