C# 我在ASP.Net核心应用程序中使用弱密码时出错

C# 我在ASP.Net核心应用程序中使用弱密码时出错,c#,asp.net-core,asp.net-identity,identity,production-environment,C#,Asp.net Core,Asp.net Identity,Identity,Production Environment,在PC上以开发模式测试应用程序时不会发生此错误,但在远程服务器上会发生此错误。应用程序会验证用户是否存在以及密码要求,但如果我使用类似“1234”的密码,应用程序会显示此错误,但如果我使用类似“@juan147-lop4s785”的密码,则会发生此错误 这是launchSettings.json文件 { "iisSettings": { "windowsAuthentication": false, "anonymousAuthe

在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"
   }
  }
 }
}
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();
        });
    }
  }
}
这是一个us mi
Startup.cs
文件

{
 "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"
   }
  }
 }
}
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();
        });
    }
  }
}
名称空间作业
{
公营创业
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”));
services.AddDefaultIdentity(options=>options.SignIn.RequireConfirmedAccount=true)
//包括通常情况下的角色和应用程序
.AddRoles()
.AddEntityFrameworkStores();
services.AddControllersWithViews();
services.AddRazorPages();
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境)
{
app.UseDeveloperExceptionPage();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseSession();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(端点=>
{
endpoints.MapControllerRoute(
名称:“默认”,
模式:“{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);

您的错误与强密码或弱密码无关,您遇到SQL异常,必须先将数据插入包含主键的父表,然后再尝试将数据插入包含外键的子表。换句话说,您正在尝试插入一条记录,该记录的外键列中的值在外键表中不存在,但是,我对其进行了测试,它仅在使用弱密码时发生。。。您还有其他想法吗?您最有可能在创建密码较弱的用户时出错,并且不检查是否成功。查看默认密码选项以及如何自定义它们。非常感谢,我在您的帮助下发现了错误。我试图在验证之前分配角色。没错,那是我的问题。谢谢