예제 #1
0
        public void Map(Type type, DataRow row, PropertyInfo prop, object entity, int ttl = 1)
        {
            string columnName = MappingHelper.GetSourceNames(type, prop.Name).First();

            if (!String.IsNullOrWhiteSpace(columnName) && row.Table.Columns.Contains(columnName))
            {
                var propertyValue = row[columnName];

                if (propertyValue != DBNull.Value)
                {
                    if (MappingHelper.ParsePrimitive(prop, entity, row[columnName]) == false)
                    {
                        Type valueType = prop.PropertyType;

                        if (valueType.Name == prop.Name)
                        {
                            string     nameSpace = EntityMapperConfig.getConfigElement("entity namespace");
                            Type       t         = Type.GetType(nameSpace + "." + prop.Name);
                            BaseEntity be        = (BaseEntity)Activator.CreateInstance(t);
                            be.Id = (int)propertyValue;
                            prop.SetValue(entity, be);
                        }
                    }
                }
            }
        }
예제 #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            EntityMapperConfig.InitDefaultSetting();
            EntityMapperConfig.ConnectionStringName = "NewCrmDatabase";

            services.AddTransient <IUserServices, UserServices>();
            services.AddTransient <ISecurityServices, SecurityServices>();
            services.AddTransient <IAppServices, AppServices>();
            services.AddTransient <IDeskServices, DeskServices>();
            services.AddTransient <IWallpaperServices, WallpaperServices>();
            services.AddTransient <ILoggerServices, LoggerServices>();
            services.AddTransient <CommonNotify>();
            services.AddTransient <IUserContext, UserContext>();
            services.AddTransient <IAppContext, Domain.Services.BoundedContext.AppContext>();
            services.AddTransient <IDeskContext, DeskContext>();
            services.AddTransient <ILoggerContext, LoggerContext>();
            services.AddTransient <IMemberContext, MemberContext>();
            services.AddTransient <ISecurityContext, SecurityContext>();
            services.AddTransient <IWallpaperContext, WallpaperContext>();
            services.AddAntiforgery();

            services.AddCors(options => options.AddPolicy("cors", p => p.SetIsOriginAllowed(_ => true)
                                                          .AllowAnyHeader()
                                                          .AllowAnyMethod()
                                                          .AllowCredentials()));
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "API Demo", Version = "v1"
                });
                c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
            });
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer           = true,                                                                    //是否验证Issuer
                    ValidateAudience         = true,                                                                    //是否验证Audience
                    ValidateLifetime         = true,                                                                    //是否验证失效时间
                    ClockSkew                = TimeSpan.FromSeconds(30),
                    ValidateIssuerSigningKey = true,                                                                    //是否验证SecurityKey
                    ValidAudience            = Appsetting.Domain,                                                       //Audience
                    ValidIssuer              = Appsetting.Domain,                                                       //Issuer,这两项和前面签发jwt的设置一致
                    IssuerSigningKey         = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Appsetting.SecurityKey)) //拿到SecurityKey
                };
                options.Events = new JwtBearerEvents
                {
                    OnChallenge = context =>
                    {
                        //此处代码为终止.Net Core默认的返回类型和数据结果,这个很重要哦,必须
                        context.HandleResponse();

                        var payload = JsonSerializer.Serialize(new ResponseBase
                        {
                            HttpStatusCode = StatusCodes.Status401Unauthorized,
                            IsSuccess      = false,
                            Message        = "认证过期,清重新登陆"
                        });
                        context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
                        context.Response.ContentType = "application/json";
                        context.Response.WriteAsync(payload);
                        return(Task.FromResult(0));
                    }
                };
            });

            services.AddControllers()
            .AddJsonOptions(options =>
            {
                options.JsonSerializerOptions.PropertyNamingPolicy = null;
                options.JsonSerializerOptions.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
                options.JsonSerializerOptions.Converters.Add(new NewCrmCore.Infrastructure.Converter.BooleanConverter());
                options.JsonSerializerOptions.Converters.Add(new NewCrmCore.Infrastructure.Converter.Int32Converter());
            });
            services.AddSignalR();
        }