我在访问O(n * sqrt(n) + k)
类中的k
和RoleManager
时遇到问题。这是我用来注册UserManager
的代码:
Startup.cs
用户是我的自定义类,继承自Identity
,并通过其他一些属性进行了扩展。
我在services.AddIdentity<User, IdentityRole>(cfg =>
{
cfg.User.RequireUniqueEmail = true;
cfg.SignIn.RequireConfirmedEmail = true;
})
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<CarinioContext>()
.AddDefaultTokenProviders()
.AddErrorDescriber<CustomIdentityErrorDescriber>();
类中有一个函数来创建角色和用户。我知道这对播种不利,我会在将来进行更改,但我需要首先使其以这种方式工作。这是我的代码:
IdentityUser
程序总是崩溃说:
startup.cs
private async Task CreateRoles()
{
//adding custom roles
var UserManager = _serviceProvicer.GetRequiredService<UserManager<IdentityUser>>();
var RoleManager = _serviceProvicer.GetRequiredService<RoleManager<IdentityRole>>();
string[] roleNames = { "Admin", "Client", "Driver" };
IdentityResult roleResult;
foreach (var roleName in roleNames)
{
//creating the roles and seeding them to the database
var roleExist = await RoleManager.RoleExistsAsync(roleName);
if (!roleExist)
{
roleResult = await RoleManager.CreateAsync(new IdentityRole(roleName));
}
}
//creating a super user who could maintain the web app
var poweruser = new User
{
UserName = _configuration.GetSection("UserSettings")["UserName"],
Email = _configuration.GetSection("UserSettings")["UserEmail"],
PhoneNumber = _configuration.GetSection("UserSettings")["PhoneNumer"],
};
string UserPassword = _configuration.GetSection("UserSettings")["UserPassword"];
var _user = await UserManager.FindByEmailAsync(_configuration.GetSection("UserSettings")["UserEmail"]);
if (_user == null)
{
var createPowerUser = await UserManager.CreateAsync(poweruser, UserPassword);
if (createPowerUser.Succeeded)
{
//here we tie the new user to the "Admin" role
await UserManager.AddToRoleAsync(poweruser, "Admin");
}
}
}
也是如此:
"One or more errors occurred. (No service for type 'Microsoft.AspNetCore.Identity.RoleManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' has been registered.)"
我的猜测是我没有正确访问UserManager
和"One or more errors occurred. (No service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' has been registered.)"
。有人知道解决方案吗?
答案 0 :(得分:0)
dotnet核心具有出色的功能,可在第一时间为角色或用户提供种子 在efcore的entityconfiguration部分中,您可以使用打击类创建默认角色:
public class RoleConfiguration : IEntityTypeConfiguration<Role>
{
public void Configure(EntityTypeBuilder<Role> builder)
{
builder.HasData(new Role() {Id=1, Name = "Manager", NormalizedName = "MANAGER",Title="manager" }, new Role() {Id=2, Name = "Student", NormalizedName = "STUDENT",Title="student" });
}
}
答案 1 :(得分:0)
尝试修改您的代码:
var UserManager = _serviceProvicer.GetRequiredService<UserManager<IdentityUser>>();
至:
var UserManager = _serviceProvicer.GetRequiredService<UserManager<User>>();
如果您创建继承自IdentityUser
的自定义类,则需要在User
中注册新的ConfigureServices
:
services.AddIdentity<User, IdentityRole>
在CarinioContext
中类似:
public class CarinioContext: IdentityDbContext<User, IdentityRole, string>
在_LoginPartial.cshtml
中:
@inject SignInManager<User> SignInManager
@inject UserManager<User> UserManager