Ejemplo n.º 1
0
 // This method gets called by the runtime. Use this method to add services to the container.
 // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
 public void ConfigureServices(IServiceCollection services)
 {
     //
     services.AddIdentityServer()
     .AddDeveloperSigningCredential()
     .AddInMemoryApiResources(IS4Config.GetApiResources())
     .AddInMemoryClients(IS4Config.GetClients());
 }
Ejemplo n.º 2
0
 public Task <Client> FindClientByIdAsync(string clientId)
 {
     return(Task.FromResult(IS4Config.GetClients().Single((Client c) => c.ClientId == clientId)));
 }
Ejemplo n.º 3
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContextPool <GHDbContext>(option =>
            {
                option.UseMySql(Configuration["DbConnString"]);
            });
            //允许跨域请求
            services.AddCors(option => option.AddPolicy("cors",
                                                        policy => policy.AllowAnyHeader().AllowAnyMethod().AllowCredentials().WithOrigins(new[] { "http://xxx.xxx.com" })));

            //注册IS4服务
            var is4Buider = services.AddIdentityServer()
                            .AddDeveloperSigningCredential()
                            .AddInMemoryApiResources(IS4Config.GetApiResources())           //IS4 导入定义的应用资源API
                            .AddInMemoryIdentityResources(IS4Config.GetIdentityResources()) //IS4 自身API
                            .AddInMemoryClients(IS4Config.GetClients())                     //IS4 导入定义的客户端
                            .AddResourceOwnerValidator <IS4UserValidator>()                 //IS4 自数据库验证用户类
                            .AddProfileService <IS4ProfileService>();                       //IS4 自数据库验证用户类


            //注册验证(*用于被保护的API资源,与IS4无关* )
            string ProtectApiUrl = Configuration["ProtectApiUrl"];

            services.AddAuthentication("Bearer").AddJwtBearer(r =>
            {
                //是否必需HTTPS
                r.RequireHttpsMetadata = false;
                //认证服务地址(由于本项目APi资源与IS4服务器均在一起,故地址相同)
                r.Authority = ProtectApiUrl;
                //权限标识
                r.Audience = "PlatformApis";
            });

            services.AddControllers().AddNewtonsoftJson(options =>
            {
                //忽略循环引用
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                //不更改元数据的key的大小写
                options.SerializerSettings.ContractResolver = new DefaultContractResolver();
                //设置时间格式
                options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
            })
            .AddXmlDataContractSerializerFormatters()      //添加XML数据格式支持
            .ConfigureApiBehaviorOptions(setup =>
            {
                //自定义验证错误信息。
                setup.InvalidModelStateResponseFactory = context =>
                {
                    var problemDetails = new ValidationProblemDetails(context.ModelState)
                    {
                        Type     = "Office/Work/Platform/Api",
                        Title    = "模型状态错误",
                        Status   = StatusCodes.Status422UnprocessableEntity,
                        Detail   = "一般为数据模型绑定时验证错误!",
                        Instance = context.HttpContext.Request.Path
                    };
                    problemDetails.Extensions.Add("TraceId", context.HttpContext.TraceIdentifier);
                    return(new UnprocessableEntityObjectResult(problemDetails)
                    {
                        ContentTypes = { "applaication/problem+json" }
                    });
                };
            });

            services.Configure <Microsoft.AspNetCore.Http.Features.FormOptions>(x =>
            {
                x.ValueLengthLimit         = int.MaxValue; //设置表单键值对中值的长度限制
                x.MultipartBodyLengthLimit = int.MaxValue; //设置文件上传的大小限制
                x.MemoryBufferThreshold    = int.MaxValue; //设置multipart头长度的限制
            });

            services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

            // Register the Swagger generator, defining 1 or more Swagger documents
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version     = "Ver:1.0.0",
                    Title       = "Office.Work.Platform.Api",
                    Description = "政工业务平台API服务,包括:人员信息、劳资管理等"
                });
            });
        }