Esempio n. 1
0
        public static async Task Main(string[] args)
        {
            var builder = WebAssemblyHostBuilder.CreateDefault(args);

            builder.RootComponents.Add <App>("app");

            builder.Services.AddTransient(sp => new HttpClient {
                BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)
            });
            builder.Services.AddBlazoredModal();

            builder.Services.AddBlazoredLocalStorage();
            builder.Services.AddAuthorizationCore(o =>
            {
                o.AddPolicy(ShoppingUserPolicies.IsAdmin, ShoppingUserPolicies.IsAdminPolicy());
                o.AddPolicy(ShoppingUserPolicies.IsProductModifier, ShoppingUserPolicies.IsProductModifierPolicy());
                o.AddPolicy(ShoppingUserPolicies.IsProductCategoryModifier, ShoppingUserPolicies.IsProductCategoryModifierPolicy());
                o.AddPolicy(ShoppingUserPolicies.IsUserManager, ShoppingUserPolicies.IsUserManagerPolicy());
                o.AddPolicy(ShoppingUserPolicies.IsUserCreator, ShoppingUserPolicies.IsUserCreatorPolicy());
                o.AddPolicy(ShoppingUserPolicies.IsUserRoleManager, ShoppingUserPolicies.IsUserRoleManagerPolicy());
                o.AddPolicy(ShoppingUserPolicies.IsDatabaseManager, ShoppingUserPolicies.IsDatabaseManagerPolicy());
            });

            builder.Services.AddScoped <AuthenticationStateProvider, ApiAuthenticationStateProvider>();
            builder.Services.AddScoped <IAuthService, AuthService>();

            builder.Services.AddScoped <ITokenProvider, TokenProviderLocalStorage>();
            builder.Services.AddTransient <IProductCategories, ProductCategoryApiAccess>();
            builder.Services.AddTransient <IProducts, ProductsApiAccess>();
            builder.Services.AddTransient <IShoppingLists, ShoppingListsApiAccess>();
            builder.Services.AddTransient <IUserGroupRepository, UserGroupsApiAccess>();
            builder.Services.AddTransient <IUserGroupShoppingLists, UserGroupsShoppingListsApiAccess>();
            builder.Services.AddTransient <IStoreRepository, StoreApiAccess>();
            builder.Services.AddTransient <IStoreChainRepository, StoreChainApiAccess>();
            builder.Services.AddTransient <IStoreProductCatRepository, StoreProductCatApiAccess>();

            builder.Services.AddTransient <IUserRepository, UserRepositoryApiAccess>();
            builder.Services.AddTransient <ICurrentUserProvider, CurrentUserApiAccess>();

            await builder.Build().RunAsync();
        }
Esempio 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)
        {
            services.AddDbContext <ApplicationDbContext>(o =>
            {
                o.UseMySql(Configuration.GetConnectionString("IdentityMySQL"),
                           sqlOptions =>
                {
                    sqlOptions.EnableRetryOnFailure(
                        maxRetryCount: 10,
                        maxRetryDelay: TimeSpan.FromSeconds(30),
                        errorNumbersToAdd: null);
                });
            });

            services.AddDefaultIdentity <ShoppingUser>()
            .AddRoles <IdentityRole>()
            .AddEntityFrameworkStores <ApplicationDbContext>();

            services.AddDbContext <ShoppingDbContext>(o =>
                                                      o.UseMySql(Configuration.GetConnectionString("Shopping_Azure")));

            services.AddHealthChecks();

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(o =>
            {
                o.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer           = true,
                    ValidateAudience         = true,
                    ValidateLifetime         = true,
                    ValidateIssuerSigningKey = true,
                    ValidIssuer      = Configuration["JwtIssuer"],
                    ValidAudience    = Configuration["JwtAudience"],
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JwtSecurityKey"]))
                };
            });
            services.AddAuthorization(o =>
            {
                o.AddPolicy(ShoppingUserPolicies.IsAdmin, ShoppingUserPolicies.IsAdminPolicy());
                o.AddPolicy(ShoppingUserPolicies.IsProductModifier, ShoppingUserPolicies.IsProductModifierPolicy());
                o.AddPolicy(ShoppingUserPolicies.IsProductCategoryModifier, ShoppingUserPolicies.IsProductCategoryModifierPolicy());
                o.AddPolicy(ShoppingUserPolicies.IsUserManager, ShoppingUserPolicies.IsUserManagerPolicy());
                o.AddPolicy(ShoppingUserPolicies.IsUserCreator, ShoppingUserPolicies.IsUserCreatorPolicy());
                o.AddPolicy(ShoppingUserPolicies.IsUserRoleManager, ShoppingUserPolicies.IsUserRoleManagerPolicy());
                o.AddPolicy(ShoppingUserPolicies.IsDatabaseManager, ShoppingUserPolicies.IsDatabaseManagerPolicy());
            });

            services.AddControllersWithViews(o =>
            {
                o.Filters.Add(typeof(ApiResponseExceptionFilter));
            });
            services.AddRazorPages();
            services.AddHttpContextAccessor();

            services.Configure <MvcOptions>(o =>
            {
                o.MaxValidationDepth = null;
            });

            services.AddTransient <IAuthService, AuthService>();
            services.AddTransient <IShoppingDataRepository, ShoppingDataRepoEf>();
            services.AddTransient <IUserRepository, UserRepository>();
            services.AddTransient <ICurrentUserProvider, UserFromHttpContextProvider>();
            services.AddTransient <IUserGroupRepository, UserGroupRepository>();
            services.AddTransient <IProductCategories, ProductCategoryRepository>();
            services.AddTransient <IProducts, ProductRepository>();
            services.AddTransient <IShoppingLists, ShoppingListRepository>();
            services.AddTransient <IUserGroupShoppingLists, UserGroupShoppingListRepository>();
            services.AddTransient <IStoreChainRepository, StoreChainRepository>();
            services.AddTransient <IStoreRepository, StoreRepository>();
            services.AddTransient <IStoreProductCatRepository, StoreProductCatRepository>();
        }