コード例 #1
0
ファイル: Startup.cs プロジェクト: drussilla/LearnWordsFast
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            RSACryptoServiceProvider myRSA = new RSACryptoServiceProvider(2048);
            RSAParameters publicKey = myRSA.ExportParameters(true);
            key = new RsaSecurityKey(publicKey);

            tokenOptions = new TokenAuthOptions
            {
                Audience = "http://localhost:5000/",
                Issuer = "http://localhost:5000/",
                SigningCredentials = new SigningCredentials(key, SecurityAlgorithms.RsaSha256Signature)
            };

            services.AddSingleton<TokenAuthOptions>(tokenOptions);

            services.AddAuthorization(auth =>
            {
                auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder()
                    .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
                    .RequireAuthenticatedUser().Build());
            });

            services.Configure<MvcJsonOptions>(options =>
            {
                if (_hostingEnv.IsDevelopment())
                {
                    options.SerializerSettings.Formatting = Formatting.Indented;
                }

                options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            });

            services.Configure<MvcOptions>(options =>
            {
                options.CacheProfiles.Add("IndexPage",
                    new CacheProfile
                    {
                        Duration = 60 * 60 * 24
                    });
            });

            services
                .AddIdentity<User, string>()
                .AddEF()
                .AddDefaultTokenProviders();

            services.AddCors();
            services.AddMvc();

            services.AddSingleton(_ => _configuration);

            services.AddEF();

            services.AddScoped<ITrainingService, TrainingService>();
            services.AddSingleton<IDateTimeService, DateTimeService>();
            services.AddScoped<ITrainingWordProvider, TrainingWordProvider>();
            services.AddScoped<ITrainingSessionFactory, TrainingSessionFactory>();
        }
コード例 #2
0
 public TokenController(TokenAuthOptions tokenOptions)
 {
     _tokenOptions = tokenOptions;
 }