【问题标题】:asp.net core 5 mvc super admin connectionasp.net core 5 mvc 超级管理员连接
【发布时间】:2026-02-14 20:05:02
【问题描述】:

请有人帮我创建 asp.net core 5 的超级管理员表单。

这是我的代码,创建了用户和角色。但是当我尝试使用凭据登录时,_signInManager.PasswordSignInAsync 方法总是返回 false。

在启动类中,我为角色和超级管理员创建了这个方法:

private async Task CreateRoles(IServiceProvider serviceprovider, RoleManager<IdentityRole> roleManager, UserManager<ApplicationUser> userManager, ApplicationDbContext dbContext)
    {            
        foreach (string rol in this.Configuration.GetSection("Roles").Get<List<string>>())
        {
            if (!await roleManager.RoleExistsAsync(rol))
            {
                await roleManager.CreateAsync(new IdentityRole(rol));
            }
                     
        }
        var poweruser = new ApplicationUser
        {

            UserName = Configuration.GetValue<string>("PowerUser:Username"),
            Email = Configuration.GetValue<string>("PowerUser:Email")
        };
        string userPWD = Configuration.GetValue<string>("PowerUser:Password");
        string useremail = Configuration.GetValue<string>("PowerUser:Email");
        var _user = await userManager.FindByEmailAsync(useremail);
        if (_user == null)
        {
            var createPowerUser = await userManager.CreateAsync(poweruser, userPWD);
            if (createPowerUser.Succeeded)
            {
                //ApplicationUser users = userManager.Users.Where(u=>u.Email.Equals("mamadous@accessbankplc.com")).SingleOrDefault();
                //here we tie the new user to the role
          
                await userManager.AddToRoleAsync(poweruser, "Admin");
                ApplicationUser update = (from db in dbContext.Users
                                       where db.Email == "syllbailo2@gmail.com"
                                       select db).SingleOrDefault();
                update.role = "Admin";
                dbContext.SaveChanges();


            }
        }
    }

但是当我尝试连接时,我收到了 var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: false); 的错误消息。

请帮忙?

【问题讨论】:

  • 您是否正确设置了身份验证?
  • 您好@Chaodeng,我的appsetting 文件配置错误,用户名和电子邮件的内容不同。现在我很好谢谢
  • @Sylla 我编辑了您的问题以更正拼写和标点符号。在发布您的问题之前,请重新阅读并进行编辑以确保清晰。在这个问题中,配置文件中的 PowerUser:Username、PowerUser:Password 和 PowerUser:Email 的 sn-p 将包含有用的上下文。如果您解决了自己的问题,请将您的解释作为答案而不是评论发布,并接受它,以便其他人知道问题已得到解决。

标签: c# asp.net-core


【解决方案1】:

我的问题是我的 appsettings.json 文件的配置值错误。用户名和电子邮件的内容不同。现在我很好。

我的appsettings.json 文件的内容:

"PowerUser": 
{
    "Username": "xxx@gmail.com",
    "Email": "xxx@gmail.com",
    "Password": "xxx"
},

我将用户名和电子邮件替换为具有相同的值,相同的电子邮件,并且现在可以使用。

谢谢。

【讨论】: