User class
:
public class User: IdentityUser
{
}
StartUp.cs
:
public class Startup
{
private readonly IConfiguration _config;
public Startup(IConfiguration config)
{
_config = config;
}
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentity<User, IdentityRole>(cfg =>
{
cfg.User.RequireUniqueEmail = true;
}).AddEntityFrameworkStores<DutchContext>();
services.AddDbContext<DutchContext>(cfg =>
{
cfg.UseSqlServer("Data Source=.;Initial Catalog=DutchDatabase;Integrated Security=True;MultipleActiveResultSets=true;");
});
services.AddAutoMapper();
services.AddTransient<IMailService, NullMailService>();
services.AddScoped<IDutchRepository, DutchRepository>();
services.AddScoped<IAccountRepository, AccountRepository>();
services.AddScoped<IOrganizationRepository, OrganizationRepostory>();
services.AddScoped<UserManager<User>, UserManager<User>>();
services.AddMvc()
.AddJsonOptions(opt => opt.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider)
{
//app.UseDefaultFiles();
if (env.IsDevelopment())
app.UseDeveloperExceptionPage();
app.UseStaticFiles();
app.UseNodeModules(env);
app.UseAuthentication();
app.UseMvc(cfg =>
{
cfg.MapRoute("Default",
"{controller}/{action}/{id?}",
new { controller = "Account", Action = "LogIn" });
});
CreateRoles(serviceProvider).Wait();
}
private async Task CreateRoles(IServiceProvider serviceProvider)
{
//initializing custom roles
var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
var UserManager = serviceProvider.GetRequiredService<UserManager<IdentityUser>>();
string[] roleNames = { "Admin", "Tester", "Developer" };
IdentityResult roleResult;
foreach (var roleName in roleNames)
{
var roleExist = await RoleManager.RoleExistsAsync(roleName);
if (!roleExist)
{
//create the roles and seed them to the database: Question 1
roleResult = await RoleManager.CreateAsync(new IdentityRole(roleName));
}
}
IdentityUser user = await UserManager.FindByEmailAsync("subratkr@gmail.com");
if (user == null)
{
user = new IdentityUser()
{
UserName = "admin@gmail.com",
Email = "admin@gmail.com",
};
await UserManager.CreateAsync(user, "Test@123");
}
await UserManager.AddToRoleAsync(user, "Admin");
IdentityUser user1 = await UserManager.FindByEmailAsync("tester@gmail.com");
if (user1 == null)
{
user1 = new IdentityUser()
{
UserName = "tester@gmail.com",
Email = "tester@gmail.com",
};
await UserManager.CreateAsync(user1, "Test@123");
}
await UserManager.AddToRoleAsync(user1, "Tester");
IdentityUser user2 = await UserManager.FindByEmailAsync("dev@gmail.com");
if (user2 == null)
{
user2 = new IdentityUser()
{
UserName = "dev@gmail.com",
Email = "dev@gmail.com",
};
await UserManager.CreateAsync(user2, "Test@123");
}
await UserManager.AddToRoleAsync(user2, "Developer");
}
}
问题获取异常:
InvalidOperationException:类型不提供服务 'Microsoft.AspNetCore.Identity.UserManager。
在线:
var UserManager = serviceProvider.GetRequiredService<UserManager<IdentityUser>>();
有人可以帮忙吗?谢谢。
答案 0 :(得分:0)
使用您的IdentityUser实现,即User类
LTChar
由于在ConfigureServices方法中,您已向身份提供者声明要使用该类var UserManager = serviceProvider.GetRequiredService<UserManager<User>>();