我有一个任务,在ASP .NET Core MVC应用程序和控制台应用程序中使用ASP .NET Identity对用户进行身份验证(问问我的大学老师为什么我们需要这个(。当我在我的"普通"ASP .NET Core MVC应用程序中使用它时,一切都很好。但是当我尝试在我的控制台应用程序中执行此操作时,它只是抛出一个错误,上面写着"HttpContext 不得为空"。在我查看了 SignInManager Microsoft 类内部后,我被吓坏了。我显然不能直接使用我的 DbContext,因为存储在那里的用户密码是散列的。你能帮我处理一下吗?
我的程序类代码(简而言之,ConfigureServices((制作DI,我的建议是我必须注入其他东西才能使其工作(:
class Program
{
private static IServiceCollection ConfigureServices()
{
IConfigurationBuilder builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false);
IConfigurationRoot configuration = builder.Build();
string connectionString = configuration["ConnectionString"];
IServiceCollection services = new ServiceCollection();
services.AddLogging();
services.AddDbContext<BreakHealContext>(options => options.UseSqlServer(connectionString, b => b.MigrationsAssembly("IOT")));
services.AddScoped<IExerciseService, ExerciseService>();
services.AddScoped<IInjuryService, InjuryService>();
services.AddScoped<IUserService, UserService>();
services.AddIdentity<User, IdentityRole>(options =>
{
options.Password.RequireDigit = false;
options.Password.RequiredLength = 4;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequireLowercase = false;
})
.AddEntityFrameworkStores<BreakHealContext>()
.AddDefaultTokenProviders();
services.Configure<IdentityOptions>(opts =>
{
opts.User.RequireUniqueEmail = true;
opts.User.AllowedUserNameCharacters =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+/";
});
return services;
}
static async Task Main(string[] args)
{
IServiceCollection services = ConfigureServices();
ServiceProvider serviceProvider = services.BuildServiceProvider();
UserService userService = (UserService)serviceProvider.GetService<IUserService>();
ExerciseService exerciseService = (ExerciseService)serviceProvider.GetService<IExerciseService>();
InjuryService injuryService = (InjuryService)serviceProvider.GetService<IInjuryService>();
UserManager<User> userManager = serviceProvider.GetRequiredService<UserManager<User>>();
SignInManager<User> signInManager = serviceProvider.GetRequiredService<SignInManager<User>>();
IotService iotService = new IotService(userService, exerciseService, injuryService, userManager, signInManager);
await iotService.Run();
Console.WriteLine("Buy buy!)");
}
以及我实际使用登录管理器的授权部分:
private async Task<string> Login()
{
while (true)
{
Console.WriteLine("Enter your email:");
string email = Console.ReadLine();
Console.WriteLine("Enter your password:");
string password = Console.ReadLine();
var user = await this.userManager.FindByNameAsync(email);
if (user != null)
{
if (await this.userManager.IsEmailConfirmedAsync(user) == false)
{
Console.WriteLine("You have not confirmed you email yet");
}
}
else
{
Console.WriteLine("No such email is registered!");
}
#region Check for password
if (user == null) continue;
var result = await this.signInManager.PasswordSignInAsync(email, password, false, false);
if (result.Succeeded)
{
if (user.IsAdmin == true)
{
Console.WriteLine(
"You can't login as an ordinary user because this email is used by an admin!");
}
else
{
return user.Id;
}
}
else
{
Console.WriteLine("Wrong email and/or password");
}
#endregion
}
}
是的,我已经通过设置 SignInManager.Context 属性完成了此操作:
signInManager.Context = new DefaultHttpContext {RequestServices = serviceProvider};