简体   繁体   English

ASP.NET Core 5 MVC 超级管理员连接

[英]ASP.NET Core 5 MVC super admin connection

Can someone help me with a superAdmin creation form in ASP.NET Core 5?有人可以帮助我在 ASP.NET Core 5 中创建超级管理员表单吗?

Here is my code.这是我的代码。 The user is created and roles too.用户已创建,角色也已创建。 But when I try to log in with the credentials, the _signInManager.PasswordSignInAsync method returns always false.但是当我尝试使用凭据登录时, _signInManager.PasswordSignInAsync方法总是返回 false。

From the startup class I created this method for roles and the super admin:在启动类中,我为角色和超级管理员创建了这个方法:

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


            }
        }
    }

But when I try to connect I'm receiving false with:但是当我尝试连接时,我收到错误消息:

var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: false);

Please any help?请问有什么帮助吗?

My issue was that I had a bad configuration value on my appsettings.json file.我的问题是我的appsettings.json文件的配置值错误。 The username and the email does not have the same content.用户名和电子邮件的内容不同。 Now I'm fine.现在我很好。

Content of my appsettings.json file:我的appsettings.json文件的内容:

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

I replaced the username and email to have the same value, the same email, and it works now.我将用户名和电子邮件替换为具有相同的值,相同的电子邮件,现在可以使用。

Thx.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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