コード例 #1
0
ファイル: Startup.cs プロジェクト: YaroslavNizhankoskyi/RxApp
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers().AddNewtonsoftJson(opt =>
            {
                opt.SerializerSettings.ReferenceLoopHandling =
                    Newtonsoft.Json.ReferenceLoopHandling.Ignore;
            });

            services.AddCors();



            services.AddAutoMapper(typeof(Startup));

            services.AddDbContextPool <RxAppContext>(
                options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddIdentity <Customer, IdentityRole>(options =>
            {
                options.Password.RequiredLength         = 6;
                options.Password.RequiredUniqueChars    = 3;
                options.Password.RequireLowercase       = false;
                options.Password.RequireDigit           = false;
                options.Password.RequireUppercase       = false;
                options.Password.RequireNonAlphanumeric = false;
                options.User.AllowedUserNameCharacters  = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+ ";
            })
            .AddSignInManager <SignInManager <Customer> >()
            .AddEntityFrameworkStores <RxAppContext>();

            services.AddTransient <IUnitOfWork, UnitOfWork>();

            services.AddAuthorization(auth =>
            {
                auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder()
                               .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
                               .RequireAuthenticatedUser().Build());
            });
            services.AddAuthentication(
                option =>
            {
                option.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                option.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(options => {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(
                        Encoding.UTF8.GetBytes(Configuration.GetSection("AppSettings:Token").Value)),
                    ValidateIssuer   = false,
                    ValidateAudience = false
                };
            });

            services.AddScoped <ArduinoData>(sp => ArduinoSession.GetData(sp));

            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.AddDistributedMemoryCache();
            services.AddSession(opts => {
                opts.IdleTimeout        = TimeSpan.FromDays(1);
                opts.Cookie.IsEssential = true;
            });
        }