Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ dotnet add package CoreAdmin
Add this line before ```var app = builder.Build();``` and after your DbContexts have been added to Services in Program.cs:

```csharp
builder.Services.AddCoreAdmin();
builder.Services.AddCoreAdmin("Administrator");
```

You need to make sure Endpoints are enabled as they don't appear to be in the default templates.
Expand All @@ -57,15 +57,26 @@ dotnet add package CoreAdmin
Add this line at the bottom of ConfigureServices() (and after your DbContexts have been added to Services) in Startup.cs:

```csharp
services.AddCoreAdmin();
services.AddCoreAdmin("Administrator");
```
## Running the app

Run your app with with /coreadmin on the end of the URL, for example https://localhost:5001/coreadmin and you'll get the app appearing as shown above.

### Security

Basic role based security is currently supported. Whilst this does not need to be set up when running the admin panel in Development mode (for testing), all other environments need this set up.
Core Admin denies access by default unless a role restriction or custom authorization method is configured. This applies in Development as well as Production.

For an intentionally unprotected local demo or test only, you can explicitly enable the Development-only bypass. This setting has no effect outside Development and should not be enabled in production or on a network-accessible development server:

```csharp
builder.Services.AddCoreAdmin(new DotNetEd.CoreAdmin.CoreAdminOptions
{
AllowUnauthenticatedAccessInDevelopment = true
});
```

For real applications, configure a role restriction or a custom authorization method instead.

#### Role based security

Expand Down
8 changes: 7 additions & 1 deletion src/DotNetEd.CoreAdmin.DemoAppDotNet10/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using DotNetEd.CoreAdmin;
using DotNetEd.CoreAdmin.DemoApp.Models;
using Microsoft.EntityFrameworkCore;

Expand All @@ -10,7 +11,12 @@
builder.Services.AddDbContext<TestDbContext>(options => options.UseInMemoryDatabase("TestDatabase"));

// add Core Admin
builder.Services.AddCoreAdmin();
// Demo-only opt-in. This bypass is effective only in Development.
// Real applications should configure a role restriction or custom authorization.
builder.Services.AddCoreAdmin(new CoreAdminOptions
{
AllowUnauthenticatedAccessInDevelopment = builder.Environment.IsDevelopment()
});

var app = builder.Build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace DotNetEd.CoreAdmin.Controllers
{
[CoreAdminAuth]
[AutoValidateAntiforgeryToken]
public class CoreAdminDataController : Controller
{
private readonly IEnumerable<DiscoveredDbSetEntityType> dbSetEntities;
Expand Down Expand Up @@ -161,7 +162,6 @@ private object GetEntityFromDbSet(string dbSetName, string id,
}

[HttpPost]
[IgnoreAntiforgeryToken]
public async Task<IActionResult> CreateEntityPost(string dbSetName, string id, [FromForm] object formData)
{
var dbSetValue = GetDbSetValueOrNull(dbSetName, out var dbContextObject, out var entityType, out var relationships);
Expand Down Expand Up @@ -207,7 +207,6 @@ await CompositeValueProvider.CreateAsync(this.ControllerContext, this.Controller
}

[HttpGet]
[IgnoreAntiforgeryToken]
public IActionResult Create(string id)
{
var dbSetValue = GetDbSetValueOrNull(id, out var dbContextObject, out var entityType, out var relationships);
Expand Down Expand Up @@ -311,7 +310,6 @@ public IActionResult DeleteEntity(string dbSetName, string id)
}

[HttpPost]
[IgnoreAntiforgeryToken]
public async Task<IActionResult> DeleteEntityPost([FromForm] DataDeleteViewModel viewModel)
{
foreach (var dbSetEntity in dbSetEntities.Where(db => db.Name.ToLowerInvariant() == viewModel.DbSetName.ToLowerInvariant()))
Expand Down
12 changes: 8 additions & 4 deletions src/DotNetEd.CoreAdmin/CoreAdminAuthFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace DotNetEd.CoreAdmin
Expand All @@ -12,7 +13,7 @@ public class CoreAdminAuthAttribute : TypeFilterAttribute
public CoreAdminAuthAttribute() : base(typeof(CoreAdminAuthFilter)) { }
}

public class CoreAdminAuthFilter : IAuthorizationFilter
public class CoreAdminAuthFilter : IAsyncAuthorizationFilter
{
private readonly IWebHostEnvironment environment;
private readonly IList<CoreAdminOptions> coreAdminOptions;
Expand All @@ -25,13 +26,16 @@ public CoreAdminAuthFilter(IWebHostEnvironment environment, IEnumerable<CoreAdmi
this.serviceProvider = serviceProvider;
}

public async void OnAuthorization(AuthorizationFilterContext context)
public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
{

bool failedSecurityCheck = true;

// in Development mode, allow bypassing security (shows a warning message)
if (environment.EnvironmentName == "Development" && !coreAdminOptions.Any(o => o.IsSecuritySet))
// Permit the Development-only bypass only when explicitly opted in.
// Missing security configuration is denied by default in every environment.
if (environment.EnvironmentName == "Development"
&& coreAdminOptions.Any(o => o.AllowUnauthenticatedAccessInDevelopment)
&& !coreAdminOptions.Any(o => o.IsSecuritySet))
{
failedSecurityCheck = false;
}
Expand Down
6 changes: 6 additions & 0 deletions src/DotNetEd.CoreAdmin/CoreAdminSecurityOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ public class CoreAdminOptions
{
public string[] RestrictToRoles { get; set; }
public Func<Task<bool>> CustomAuthorisationMethod { get; set; }

/// <summary>
/// Explicitly allows access without security configuration in Development only.
/// This option is ignored in every other environment and is disabled by default.
/// </summary>
public bool AllowUnauthenticatedAccessInDevelopment { get; set; }
public bool IsSecuritySet => (RestrictToRoles != null && RestrictToRoles.Length > 0) || CustomAuthorisationMethod != null || CustomAuthorisationMethodWithServiceProvider != null;

public string CdnPath { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/DotNetEd.CoreAdmin/Views/CoreAdminData/Create.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@using (Html.BeginForm("CreateEntityPost",
"CoreAdminData",
new { id = ViewBag.DbSetName },
FormMethod.Post, false,
FormMethod.Post, true,
new { enctype = "multipart/form-data" }))
{
@Html.Hidden("dbSetName", ViewBag.DbSetName, new { })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</div>


@using (Html.BeginForm("DeleteEntityPost", "CoreAdminData", new {}, FormMethod.Post))
@using (Html.BeginForm("DeleteEntityPost", "CoreAdminData", new {}, FormMethod.Post, true, new { }))
{
@Html.HiddenFor(m => m.DbSetName)
@Html.HiddenFor(m => m.Id)
Expand Down
2 changes: 1 addition & 1 deletion src/DotNetEd.CoreAdmin/Views/CoreAdminData/Edit.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@using (Html.BeginForm("EditEntityPost",
"CoreAdminData",
new { dbSetName = ViewBag.DbSetName, id = ViewBag.Id },
FormMethod.Post, false,
FormMethod.Post, true,
new { enctype = "multipart/form-data" }))
{

Expand Down
10 changes: 9 additions & 1 deletion tests/DotNetEd.CoreAdmin.IntegrationTests/BasicTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DotNetEd.CoreAdmin.IntegrationTests.TestApp;
using DotNetEd.CoreAdmin;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -17,7 +18,14 @@ public BasicTests(IntegrationTestsWebHostFactory<IntegrationTestStartup> factory
_factory = factory;
}

static void ConfigureTestServices(IServiceCollection services) { }
static void ConfigureTestServices(IServiceCollection services)
{
// These UI smoke tests explicitly opt into the Development-only bypass.
services.AddCoreAdmin(new CoreAdminOptions
{
AllowUnauthenticatedAccessInDevelopment = true
});
}

[Fact]
public async Task ShowsTestEntitiesOnScreenOnAdminScreen()
Expand Down
Loading