c# - 在 ASP.Net Core 应用程序中使用弱密码时出现错误
问题描述
当我在 PC 上以开发模式测试应用程序时,不会发生此错误,但在远程服务器上会发生。该应用程序验证用户是否存在以及密码要求,但如果我使用像“1234”这样的密码,应用程序会给我这个错误,但如果我使用像“@juan147-lop4s785”这样的密码,它确实会发生
这是launchSettings.json
文件
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:63322",
"sslPort": 44361
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
//"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"UniJobs": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
//"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
还有这个我们的 miStartup.cs
文件
namespace UniJobs
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<Usuarios>(options => options.SignIn.RequireConfirmedAccount = true)
//Incluye los roles de los usuarios a la app
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddControllersWithViews();
services.AddRazorPages();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseDeveloperExceptionPage();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseSession();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
}
}
}
解决方案
很可能在创建用户时,您没有检查它是否成功,因此当您的“弱密码”不符合默认身份选项时,它无法创建用户,然后数据库抱怨丢失外键,导致你的错误。
在此处查看默认身份密码选项以及如何自定义它们。
如果您UserManager
用于创建用户,您只需要检查它是否有任何错误并做出相应的响应,如下所示:
var result = await UserManager.CreateAsync(user, password);
if(!result.Succeeded)
return result.Errors.Select(x => x.Description);
var roleResult = await UserManager.AddToRoleAsync(user, "User");
if (!roleResult.Succeeded)
return roleResult.Errors.Select(x => x.Description);
推荐阅读
- python - 熊猫加载csv文件ValueError
- android - 使用阿拉伯语 Reshaper/KIVY 的 Android APP 崩溃
- numpy - 如何生成多元正态分布?
- r - dplyr::mutate 与外部函数 - 返回 0
- java - 有api调用时如何在不重复代码的情况下膨胀progressBar布局
- haskell - 如何组合两个组合的应用函子?
- python - 如何按python字典中值的最后一个字母排序?
- django - 多租户袜子
- android - 无法使用自定义 buildType - 活动构建变体“调试”没有测试工件
- sql - 自内连接、聚合和集合的总和