public async Task <IActionResult> InitAsync() { await _roleService.CreateRoleAsync("Administrator"); var roles = await _roleService.GetRolesAsync(); var administrator = roles.First(); var userId = await _userService.RegisterAsync(_configuration["InitializeSettings:AdministratorEmail"], _configuration["InitializeSettings:DefaultPassword"]); await _userService.ConfirmAsync(userId, true); await _roleService.SetUserRolesAsync(userId, new List <int> { administrator.Id }); var permissions = BlogPermissionsExtensions.GetPermissions(); await _roleService.GrantRolePermissionsAsync(administrator.Id, permissions); return(Ok("Initialize Succeed")); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddFeatureManagement(Configuration.GetSection("FeatureManagementSettings")); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options => { options.RequireHttpsMetadata = true; options.TokenValidationParameters = new TokenValidationParameters { ValidateAudience = true, ValidateIssuer = true, ValidAudience = Configuration["JwtOptions:Audience"], ValidIssuer = Configuration["JwtOptions:Issuer"], IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JwtOptions:Key"])) }; }); services.AddAuthorization(options => { var permissions = BlogPermissionsExtensions.GetPermissions(); permissions.ForEach(p => options.AddPolicy(p, policy => policy.AddRequirements(new OperationAuthorizationRequirement { Name = p }))); }); services.AddSingleton <IAuthorizationHandler, PermissionAuthorizationHandler>(); SqlMapper.AddTypeHandler(typeof(List <int>), new JsonTypeHandler()); SqlMapper.AddTypeHandler(typeof(List <string>), new JsonTypeHandler()); services.AddScoped <IDateTimeResolver>(d => new DateTimeResolver(TimeSpan.FromHours(8).ToString())); services.Configure <BlogSettings>(Configuration.GetSection("BlogSettings")); services.Configure <EmailSettings>(Configuration.GetSection("EmailSettings")); services.Configure <TencentCloudSettings>(Configuration.GetSection("TencentCloudSettings")); services.AddTransient <IRepository, Repository>(); services.AddTransient <IUserService, UserService>(); services.AddTransient <IRoleService, RoleService>(); services.AddTransient <ICategoryService, CategoryService>(); services.AddTransient <IPostService, PostService>(); services.AddTransient <IMenuService, MenuService>(); services.AddTransient <IEmailService, EmailService>(); services.AddCors(opt => { opt.AddPolicy("default", builder => { builder.WithOrigins(Configuration["Cors:Origin"].Split(";")) .AllowAnyHeader() .AllowAnyMethod() .AllowCredentials(); }); }); services.AddCos(); services.AddAgileConfig(); services.AddControllersWithViews(options => { options.SuppressAsyncSuffixInActionNames = false; }) .ConfigureApiBehaviorOptions(options => { options.InvalidModelStateResponseFactory = context => { var error = context.ModelState.BuildErrors(); return(new BadRequestObjectResult(error)); }; }) .AddRazorRuntimeCompilation(); }
public IActionResult GetPermissions() { return(Ok(BlogPermissionsExtensions.GetPermissions())); }