以下是我设置 Piranha CMS + Angular 的方法。我的要求是将 Piranha 作为无头 CMS 运行,Angular 负责前端。
撰写本文时我的环境:
- .NET 5.0
- 食人鱼 9.0.1
- Piranha.AspNetCore.Identity 9.0.0
- Piranha.AspNetCore.Identity.PostgreSQl 9.0.0
- Piranha.Data.EF.PostgreSql 9.0.0
- Piranha.Manager 9.0.1
- 像 Håkan Edling 提到的那样设置空食人鱼项目。
dotnet new -i Piranha.Templates
dotnet new piranha.empty
- 为 Piranha CMS 和应用程序设置身份。棘手的情况是,如果您安装 Piranha.AspNetCore.Identity,该包会覆盖身份配置,从而难以自定义您自己的用户和角色。我的方法是将 Piranha Identity 和 Application Identity 分开。
首先,设置食人鱼身份
options.UseIdentityWithSeed<IdentityPostgreSQLDb>(
db => db.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")),
ConfigureIdentity,
cookieOptions: co => // This part is optional
{
co.Cookie.Name = "YourOwnCookieName";
co.LoginPath = "/manager/login";
co.AccessDeniedPath = "/manager/login";
...
}
);
接下来,设置应用程序身份。在这里,我使用 ApplicationUser 作为您应用程序的自定义用户。
var identityBuilder = services.AddIdentityCore<ApplicationUser>(ConfigureIdentity);
identityBuilder = new IdentityBuilder(identityBuilder.UserType, typeof(IdentityRole), identityBuilder.Services);
identityBuilder.AddEntityFrameworkStores<ApplicationDbContext>();
identityBuilder.AddRoleValidator<RoleValidator<IdentityRole>>();
identityBuilder.AddRoleManager<RoleManager<IdentityRole>>();
identityBuilder.AddSignInManager<SignInManager<ApplicationUser>>();
identityBuilder.AddDefaultTokenProviders();
这是 ConfigureIdentity 的代码,您可以根据需要对其进行自定义。
private void ConfigureIdentity(IdentityOptions options)
{
options.Tokens.PasswordResetTokenProvider = TokenOptions.DefaultPhoneProvider;
options.Password.RequireDigit = false;
options.Password.RequiredLength = 5;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireLowercase = false;
options.Password.RequireUppercase = false;
options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
options.User.RequireUniqueEmail = false;
}
由于我使用 PostgreSql 作为数据库,Piranha Identity 和您的 Identity 之间存在涉及索引名称的冲突,要解决这些问题,您可以在 ApplicationDbContext OnModelCreating 中使用此代码重命名索引
builder.Entity<IdentityRole>().HasIndex(e => e.NormalizedName).HasDatabaseName("role_name_index");
builder.Entity<ApplicationUser>().HasIndex(e => e.NormalizedEmail).HasDatabaseName("email_index");
builder.Entity<ApplicationUser>().HasIndex(e => e.NormalizedUserName).HasDatabaseName("user_name_index");
此时,您有两个 Identity 正在运行。如果您设置迁移,Piranha 和您的应用程序都使用相同的 EFMigrationsHistory 表。如果您对此不满意并想分离迁移表,可以在设置 ApplicationDbContext 服务时添加此代码:
services.AddDbContext<ApplicationDbContext>(options =>
options
.UseNpgsql(
Configuration.GetConnectionString("DefaultConnection"),
builder =>
{
builder.MigrationsHistoryTable("__ef_migrations_history"); // separate migrations table
}
)
.UseSnakeCaseNamingConvention() // this is optional, from EFCore.NamingConventions package
);
Piranha 不适用于 EFCore.NamingConventions 包,例如,如果您想使用 snake_case 命名。这里有很多选择:
- 将 Piranha CMS 数据库与您自己的数据库分开(您可以滚动自己的命名约定,保留 Piranha 命名约定)。
- 使用与 Piranha CMS 相同的命名约定。
- 将 Piranha CMS 数据库命名约定与您的应用程序命名约定分开(丑陋,但这是我使用的那个)。
如果您使用选项 3 运行,在我的情况下,我希望为我的应用程序表命名为 snake_case,您必须在 ApplicationDbContext OnModelCreating 中重命名您的应用程序身份表,因为 EFCore.NamingConventions 不会重命名您的身份表。
builder.Entity<ApplicationUser>().ToTable("asp_net_users");
builder.Entity<IdentityRole>().ToTable("asp_net_roles");
builder.Entity<IdentityUserToken<string>>().ToTable("asp_net_user_tokens");
builder.Entity<IdentityUserLogin<string>>().ToTable("asp_net_user_logins");
builder.Entity<IdentityUserClaim<string>>().ToTable("asp_net_user_claims");
builder.Entity<IdentityUserRole<string>>().ToTable("asp_net_user_roles");
builder.Entity<IdentityRoleClaim<string>>().ToTable("asp_net_role_claims");
- 此时,Piranha CMS 将与您的应用程序分开运行。您可以安装 Piranha.WebApi 包以从 API 访问食人鱼内容,也可以滚动您自己的 API。 Piranha.WebApi 的代码很简单,您可以轻松地从他们的存储库中自定义代码:https://github.com/PiranhaCMS/piranha.core/tree/master/core/Piranha.WebApi。如果您不关心您的内容授权,您可以添加此选项以启用匿名访问:
services.AddPiranha(options =>
{
options.UseApi(o => o.AllowAnonymousAccess = true);
});
最后,你可以禁用 Piranha 路由
services.AddPiranha(options =>
{
options.DisableRouting();
});