コード例 #1
0
ファイル: Startup.cs プロジェクト: rolledback/RestaurantMap
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            services.Configure <StorageSettings>(options =>
            {
                options.AccountName = Configuration.GetSection("SecretSettings:BackupDbStorageAccountName").Value;
                options.AccountKey  = Configuration.GetSection("SecretSettings:BackupDbStorageAccountKey").Value;
            });

            var tokenIssuerSigningKey = Configuration.GetSection("SecretSettings:TokenIssuerSigningKey").Value;

            services.Configure <SecretSettings>(options =>
            {
                options.TokenIssuerSigningKey = tokenIssuerSigningKey;
                options.Salt = Configuration.GetSection("SecretSettings:Salt").Value;;
            });

            services.AddTransient <IRestaurantRepository, LiteDB.RestaurantRepository>();
            services.AddTransient <IUserRepository, UserRepository>();
            services.AddTransient <IBackupDbService, BackupDbService>();

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
            {
                options.TokenValidationParameters =
                    new TokenValidationParameters
                {
                    ValidateIssuer           = true,
                    ValidateAudience         = true,
                    ValidateLifetime         = true,
                    ValidateIssuerSigningKey = true,

                    ValidIssuer      = "restaurantmap.security.bearer",
                    ValidAudience    = "restaurantmap.security.bearer",
                    IssuerSigningKey = JwtSecurityKey.Create(tokenIssuerSigningKey)
                };
            });

            services.AddAuthorization(options =>
            {
                options.AddPolicy("RestaurantCreate", policy => policy.RequireClaim("RestaurantCreate"));
                options.AddPolicy("UsersManager", policy => policy.RequireClaim("UsersManager"));
                options.AddPolicy("DbManager", policy => policy.RequireClaim("DbManager"));
            });

            services.AddSingleton <IScheduledTask, BackupDbTask>();
            services.AddScheduler((sender, args) =>
            {
                Console.Write(args.Exception.Message);
                args.SetObserved();
            });
        }
コード例 #2
0
        private JwtToken GenerateAccessToken(DbUser user)
        {
            var builder = new JwtTokenBuilder()
                          .AddSecurityKey(JwtSecurityKey.Create(_SecretSettings.Value.TokenIssuerSigningKey))
                          .AddSubject("authentication")
                          .AddIssuer("restaurantmap.security.bearer")
                          .AddAudience("restaurantmap.security.bearer")
                          .AddExpiry(180);

            foreach (Permission permission in user.permissions)
            {
                builder.AddClaim(permission.associatedClaim, "True");
            }
            return(builder.Build());
        }