Exemple #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.AddMvc(x =>
            {
                x.Filters.Add(typeof(GlobalResultFilter));
            }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            services.AddDapper(Configuration.GetConnectionString("WriteConnection"), Configuration.GetConnectionString("ReadConnection"));
            services.AddDbContext <AccountInfoDbContext>(options => options.UseMySql(Configuration.GetConnectionString("WriteConnection")));

            CommonServiceConfig commonServiceConfig = new CommonServiceConfig();

            commonServiceConfig.Key    = Configuration["CommonServiceAPI:appKey"];
            commonServiceConfig.Path   = Configuration["CommonServiceAPI:CommonServicePath"];
            commonServiceConfig.Secret = Configuration["CommonServiceAPI:appsecret"];
            commonServiceConfig.Token  = Configuration["CommonServiceAPI:Token"];
            services.AddCommonService(commonServiceConfig);

            //映射
            services.AddAutoMapper(cfg =>
            {
                AutoMapperInitialize.InitServiceMap(cfg);
            });

            services.AutoDependency(typeof(AccountInfoServices), typeof(AccountInfoRepositories));//自动注入
            services.AddTransient <IAccountInfoServices, AccountInfoServices>();
            services.AddTransient <IAccountInfoRepositories, AccountInfoRepositories>();

#if DEBUG
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("workorder", new Swashbuckle.AspNetCore.Swagger.Info
                {
                    Version     = "V1",
                    Title       = "罗基工单管理系统",
                    Description = "Api说明以及测试"
                });
            });
            services.ConfigureSwaggerGen(c =>
            {
                c.IncludeXmlComments(Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "Sino.LogisticsWorkOrderManage.Web.xml"));
                c.IncludeXmlComments(Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "Sino.LogisticsWorkOrderManage.Core.xml"));
            });
#endif
        }
Exemple #2
0
 public void Setup()
 {
     AutoMapperInitialize.Initialize();
 }
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext <ApplicationContext>(options =>
            {
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
                options.UseOpenIddict();
            });
            // Add membership
            services.AddIdentity <IdentityUser, IdentityRole>(options =>
            {
                // Password settings
                options.Password.RequireDigit           = false;
                options.Password.RequireLowercase       = false;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequireUppercase       = false;
                options.Password.RequiredLength         = 6;
                options.User.AllowedUserNameCharacters  = null;

                // Confirmation email required for new account
                options.SignIn.RequireConfirmedEmail = true;

                // Lockout settings
                options.Lockout.DefaultLockoutTimeSpan  = TimeSpan.FromMinutes(30);
                options.Lockout.MaxFailedAccessAttempts = 5;
            })
            .AddEntityFrameworkStores <ApplicationContext>()
            .AddDefaultTokenProviders();

            // Register the OAuth2 validation handler.
            services.AddAuthentication(o =>
            {
                o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(options =>
            {
                options.Audience                  = "resource-server";
                options.Authority                 = "http://localhost:59515/";
                options.RequireHttpsMetadata      = false;
                options.IncludeErrorDetails       = true;
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    NameClaimType = OpenIdConnectConstants.Claims.Subject,
                    RoleClaimType = OpenIdConnectConstants.Claims.Role
                };
            });

            // Configure Identity to use the same JWT claims as OpenIddict instead
            // of the legacy WS-Federation claims it uses by default (ClaimTypes),
            // which saves you from doing the mapping in your authorization controller.
            services.Configure <IdentityOptions>(options =>
            {
                options.ClaimsIdentity.UserNameClaimType = OpenIdConnectConstants.Claims.Name;
                options.ClaimsIdentity.UserIdClaimType   = OpenIdConnectConstants.Claims.Subject;
                options.ClaimsIdentity.RoleClaimType     = OpenIdConnectConstants.Claims.Role;
            });

            // Register the OpenIddict services.
            services.AddOpenIddict(options =>
            {
                // Register the Entity Framework stores.
                options.AddEntityFrameworkCoreStores <ApplicationContext>();
                // Register the ASP.NET Core MVC binder used by OpenIddict.
                // Note: if you don't call this method, you won't be able to
                // bind OpenIdConnectRequest or OpenIdConnectResponse parameters.
                options.AddMvcBinders();
                // Enable the token endpoint.
                options.EnableTokenEndpoint("/connect/token");
                // Enable the password flow.
                options.AllowPasswordFlow();
                // During development, you can disable the HTTPS requirement.
                options.DisableHttpsRequirement();

                options.UseJsonWebTokens();
                options.AddEphemeralSigningKey();
            });


            AutoMapperInitialize.Initialize();
            services.AddCors();
            services.AddMvc()
            .AddJsonOptions(opts =>
            {
                // Force Camel Case to JSON
                opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            });

            // Without this controller actions are not forbidden if other roles are trying to access
            services.AddSingleton <IAuthenticationSchemeProvider, CustomAuthenticationSchemeProvider>();
            services.AddSingleton(Configuration);
        }