【问题标题】:How can I get data from the Identity table - AspNetUsers?如何从 Identity 表中获取数据 - AspNetUsers?
【发布时间】:2021-07-15 14:25:32
【问题描述】:

项目:

  • ASP.NET 5 + 使用身份
  • MySQL 中的数据库

为了使用数据库,我使用 Pomelo.EntityFrameworkCore.MySQL 包。

使用 ApplicationUser.cs 模型,我将字段添加到标准身份表 - AspNetUsers

注册用户(向表中添加了两个条目/用户)。

UserController.cs 控制器中,我试图从 AspNetUsers 表中获取数据。

但是 _db.applicationuser 没有返回我元素(count = 0)。

还有:

我做错了什么?

如何从 Identity 表 - AspNetUsers 中获取数据?

我做了一个例子,但是那里使用了 MS SQL 数据库。我找到了更多示例,但在这些情况下,项目甚至都没有开始。

数据/ApplicationDbContext.cs

    namespace localizeTest.Data
    {
        public class ApplicationDbContext : IdentityDbContext
        {
            public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
               : base(options)
            {
            }
    
            public DbSet<ApplicationUser> ApplicationUser { get; set; }
        }
    }

Models/ApplicationUser.cs

    namespace localizeTest.Models
    {
        public class ApplicationUser : IdentityUser
        {
            public string FistName { get; set; }
    
            public string MiddleName { get; set; }
    
            public string LasttName { get; set; }
    
            public DateTime Birthday { get; set; }
    
            public string UserType { get; set; }
            public string UserView { get; set; }
    
            [NotMapped]
            public  string RoleId { get; set; }
    
            [NotMapped]
            public string Role { get; set; }
    
        }
    }

Controllers/UserController.cs

    namespace localizeTest.Controllers
    {
        public class UserController : Controller
        {
            private readonly ApplicationDbContext _db;
            private readonly UserManager<IdentityUser> _userManager;
    
            public UserController(ApplicationDbContext db, UserManager<IdentityUser> userManager)
            {
                _db = db;
                _userManager = userManager;
            }
    
            public IActionResult Index()
            {
    
                var userList = _db.ApplicationUser.ToList();
                var userRole = _db.UserRoles.ToList();
                var roles = _db.Roles.ToList();
                foreach (var user in userList)
                {
                    var role = userRole.FirstOrDefault(u => u.UserId == user.Id);
                    if (role == null)
                    {
                        user.Role = "None";
                    }
                    else
                    {
                        user.Role = roles.FirstOrDefault(u => u.Id == role.RoleId).Name;
                    }
                }
    
                return View(userList);
            }
        }
    }

StartUp.cs - {...ConfigureServices...}

    public void ConfigureServices(IServiceCollection services)
            {
                services.AddLocalization(opt => { opt.ResourcesPath = "Resources"; });
                services.AddRazorPages()
                    .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
                    .AddDataAnnotationsLocalization();
                services.AddMvc()
                    .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
                    .AddDataAnnotationsLocalization();
    
                services.Configure<RequestLocalizationOptions>(
                opt =>
                {
                    var suppoortedCulteres = new List<CultureInfo>
                    {
                        new CultureInfo("ru"),
                        new CultureInfo("en")
                    };
                    opt.DefaultRequestCulture = new RequestCulture("ru");
                    opt.SupportedCultures = suppoortedCulteres;
                    opt.SupportedUICultures = suppoortedCulteres;
                }
                );
    
                services.AddDNTCaptcha(options =>
                {
                    options.UseCookieStorageProvider();
                });
    
                //Database
                string connection = Configuration["ConnectionStrings:DefaultConnection"];
                ServerVersion version = ServerVersion.AutoDetect(connection);
    
                services.AddDbContext<ApplicationDbContext>(options =>
                    options.UseMySql(connection, version));
                services.AddDatabaseDeveloperPageExceptionFilter();
    
                services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
                    .AddEntityFrameworkStores<ApplicationDbContext>();
                services.AddControllersWithViews();
            }

附:抱歉谷歌翻译

【问题讨论】:

  • 你从数据库中获取表,检查数据库是否已经创建并且里面有一些值。
  • 在迁移的帮助下,在数据库中创建了表。 AspNetUsers 表不为空,在(使用标准注册表单)中创建了两个用户。这些用户的授权有效。

标签: .net-core asp.net-core-mvc


【解决方案1】:

在您的 Startup.cs 中,您可以尝试更改

services.AddDefaultIdentity&lt;IdentityUser&gt;

services.AddDefaultIdentity&lt;ApplicationUser&gt;

这是您的自定义用户类

【讨论】:

  • 改了,报错:An unhandled exception occurred while processing the request. InvalidOperationException: No service for type Microsoft.AspNetCore.Identity.UserManager1[Microsoft.AspNetCore.Identity.IdentityUser]'has been registered.
  • 那么您还应该在 UserController.cs 中进行更改。将所有出现的UserManager&lt;IdentityUser&gt; 更改为UserManager&lt;ApplicationUser&gt;
猜你喜欢
  • 2014-10-22
  • 2014-06-28
  • 1970-01-01
  • 2020-11-30
  • 1970-01-01
  • 2013-09-17
  • 2020-04-20
  • 2015-11-13
  • 1970-01-01
相关资源
最近更新 更多