简体   繁体   中英

I get an error when I use weak password on my in ASP.Net Core application

This error does not happen when I test the application in development mode on my PC, but on the remote server it does. The app verify if user exist and the password requirements, but if I use a password like "1234" the app give me this error, but it does happen if I use a password like "@juan147-lop4s785"

在此处输入图像描述

This is the launchSettings.json file

{
 "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"
   }
  }
 }
}

and this us mi Startup.cs file

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();
        });
    }
  }
}

It's likely that when creating a user, you're not checking whether it was successful or not and therefore when your "weak password" doesn't meet the default Identity options, it fails to create the user and then the database complains about the missing foreign key, resulting in your error.

Have a look here for the default Identity password options and how to customise them.

If you're using UserManager to create the user, you just need to check if it there were any errors and respond accordingly, something like the below:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM