Exemplo n.º 1
0
        public void ConfigureServices(IServiceCollection services)
        {
            string connString = LoverCloudDbContext.GetConnectionStringFromFile(
                _configuration.GetConnectionString("MySql"));
            string migrationAssembly = Assembly.GetExecutingAssembly().FullName;

            services.AddDbContext <LoverCloudDbContext>(config =>
            {
                config.UseMySql(
                    connString, sql => sql.MigrationsAssembly(migrationAssembly));
            });

            services.AddIdentity <LoverCloudUser, IdentityRole>(options =>
            {
                options.Password.RequiredUniqueChars    = 0;
                options.Password.RequireLowercase       = false;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequireUppercase       = false;

                options.User.AllowedUserNameCharacters = string.Empty;
            })
            .AddEntityFrameworkStores <LoverCloudDbContext>()
            .AddDefaultTokenProviders();


            var builder = services.AddIdentityServer()
                          .AddConfigurationStore(options =>
            {
                options.ConfigureDbContext = x =>
                {
                    x.UseMySql(
                        connString, sql => sql.MigrationsAssembly(migrationAssembly));
                };
            })
                          .AddOperationalStore(options =>
            {
                options.ConfigureDbContext = x =>
                                             x.UseMySql(
                    connString, sql => sql.MigrationsAssembly(migrationAssembly));
                options.EnableTokenCleanup = true;
            })
                          .AddAspNetIdentity <LoverCloudUser>();

            // not recommended for production - you need to store your key material somewhere secure
            builder.AddDeveloperSigningCredential();

            services.AddLoverCloudCors(_configuration);
        }
Exemplo n.º 2
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)
        {
            // 配置数据库
            string connString = LoverCloudDbContext.GetConnectionStringFromFile(
                _configuration.GetConnectionString("MySql"));

            services.AddDbContext <LoverCloudDbContext>(
                config => config.UseMySql(connString));

            // 配置身份认证
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(
                options => _configuration.Bind("JwtSettings", options));
            services.AddIdentityCore <LoverCloudUser>(options =>
            {
                options.Password.RequiredUniqueChars    = 0;
                options.Password.RequireLowercase       = false;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequireUppercase       = false;

                options.User.AllowedUserNameCharacters = string.Empty;
            })
            .AddEntityFrameworkStores <LoverCloudDbContext>();

            services.AddAppAuthorization();

            services.AddRepositories();
            services.AddAutoMapper(Assembly.GetExecutingAssembly());

            services.AddControllers()
            .AddNewtonsoftJson(options => ConfigNewtonsoftJson(options))
            .AddFluentValidation();

            services.AddLoverCloudCors(_configuration);

            services.AddPropetyMapping();

            services.AddSwaggerGen(options => ConfigSwagger(options));
        }