Unable to access manager after adding Piranha CMS to existing .NET Core 3.1 project
See original GitHub issueI have an existing .NET Core 3.1 project which already has a database created. It’s using Identity for authentication with users and roles already created.
I’m trying to add Pirahna CMS to the application. I want to allow admin users the ability to create pages that would be accessible via a URL slug to non-authenticated users. Ideally, the admin user wouldn’t need to log into the Piranha Manager (but even if they can with their existing account, that is fine).
I’m running into an issue where I can’t access the manager via http://mysite/manager. When I try to access the URL I see the following in the logs:
info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[2] Authorization failed. info: Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler[13] AuthenticationScheme: Identity.Application was forbidden.
I’ve read that access to the manager can be granted via roles/claims. I manually created a role in my existing Identity role table called “PiranhaAdmin” and I assigned that role to my user. However I still can’t access the manager so I’m assuming this is wrong.
I tried following the Getting Started guide to set everything up but I’m not sure if I’m missing something.
This is my code from the ConfigureServices method in Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
// Configure database context
services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseSqlServer(new SqlConnection(Configuration.GetConnectionString("DefaultConnection")),
x =>
{
x.MigrationsAssembly("MyApplication.Data");
});
});
// Configure Identity implementation
services.AddIdentity<ApplicationUser, ApplicationRole>(options => options.SignIn.RequireConfirmedAccount = false)
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders()
.AddUserManager<ApplicationUserManager>()
.AddRoleManager<ApplicationRoleManager>();
// Configure localization
// ... ommitting code related to localization configuration
// Configure controllers with Localization
services.AddControllersWithViews(options =>
{
options.Filters.Add(new MiddlewareFilterAttribute(typeof(LocalizationPipeline)));
})
.AddViewLocalization(
LanguageViewLocationExpanderFormat.Suffix,
opts => { opts.ResourcesPath = "Resources"; })
.AddDataAnnotationsLocalization();
// Configure Razor Pages with Localization
services.AddRazorPages(options => { options.Conventions.Add(new CultureTemplateRouteModelConvention()); })
.AddViewLocalization(
LanguageViewLocationExpanderFormat.Suffix,
opts => { opts.ResourcesPath = "Resources"; })
.AddDataAnnotationsLocalization()
.AddRazorRuntimeCompilation()
.AddPiranhaManagerOptions();
// Add PiranhaCMS
services.AddPiranha();
services.AddPiranhaApplication();
services.AddPiranhaBlobStorage(new StorageCredentials(Configuration["Azure:StorageAccount"],
Configuration["Azure:StorageKey"]), Configuration["Azure:ContainerName"]);
services.AddPiranhaImageSharp();
services.AddPiranhaManager();
services.AddPiranhaTinyMCE();
services.AddPiranhaMemoryCache();
services.AddPiranhaEF<SQLServerDb>(db =>
db.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
// Adding custom application services here
// ... code ommitted
}
And here is the code from my Configure method in Startup.cs:
public void Configure(
IApplicationBuilder app,
IWebHostEnvironment env,
IApi api)
{
// Initialize Piranha
App.Init(api);
// Configure cache level
App.CacheLevel = Piranha.Cache.CacheLevel.Full;
// Build content types
new ContentTypeBuilder(api)
.AddAssembly(typeof(Startup).Assembly)
.Build()
.DeleteOrphans();
// Configure editor
//Piranha.Manager.Editor.EditorConfig.FromFile("editorconfig.json");
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
// Add call to use Piranha
app.UsePiranha();
app.UseRouting();
app.UseRequestLocalization();
app.UseAuthentication();
app.UseAuthorization();
// Configure Piranha
app.UsePiranhaIdentity();
app.UsePiranhaManager();
app.UsePiranhaTinyMCE();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "LocalizedDefault",
pattern: "{culture:culture}/{controller=Home}/{action=Index}/{id?}"
);
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
endpoints.MapPiranhaManager();
});
}
At this point the connection to the database is working because I see the new Piranha tables that were created.
I’ve read the Authentication doc (https://piranhacms.org/docs/architecture/authentication) which is where I got the idea to create and assign the PiranhaAdmin role.
I also read the Authentication Extension doc (https://piranhacms.org/docs/extensions/authentication) and tried to create a custom implementation of ISecurity. I added it to DI container and placed breakpoints in each method but they’re never triggered. This was my ISecurity implementation:
public class MyAuthService : ISecurity
{
public bool Authenticate(string username, string password)
{
return true;
}
public async Task<bool> SignIn(object context, string username, string password)
{
return true;
}
public Task SignOut(object context)
{
return;
}
}
I wouldn’t expect it to trigger anything in that class because it’s not getting to the point where I can provide credentials.
So I’m not sure what I’m missing or doing wrong here. Any help is greatly appreciated.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
After assigning the Admin claim with a few others (Pages, PagesAdd) I was able to see the manager. Now I can try getting some pages setup 😃
Thanks so much for your help!
As you can see there are a lot of claims for accessing the different parts of the manager.