猿问

无法从 ASP.NET Core 2.1 控制器获取 IdentityUser 数据

我正在 ASP.NET Core 2.1 中开发一个应用程序。我正在使用带有内置用户帐户的身份。现在我正在尝试在我遇到困难的这个应用程序中开发一个用户维护模块。我无法获取控制器内的数据。这是我的代码:


ApplicationDbContext.cs:


public class ApplicationDbContext : IdentityDbContext

{

    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)

        : base(options)

    {

    }


    public DbSet<ApplicationUser> ApplicationUsers { get; set; }


}

启动.cs:


public void ConfigureServices(IServiceCollection services)

    {

        services.Configure<CookiePolicyOptions>(options =>

        {

            // This lambda determines whether user consent for non-essential cookies is needed for a given request.

            options.CheckConsentNeeded = context => true;

            options.MinimumSameSitePolicy = SameSiteMode.None;

        });


        services.AddDbContext<ApplicationDbContext>(options =>

            options.UseSqlServer(

                Configuration.GetConnectionString("DefaultConnection")));

        services.AddIdentity<IdentityUser,IdentityRole>(config => {

            config.SignIn.RequireConfirmedEmail = true;

        })

        .AddEntityFrameworkStores<ApplicationDbContext>()

        .AddDefaultUI()

        .AddDefaultTokenProviders();


        services.AddMemoryCache();

        services.AddSession();


        services.AddMvc()

            .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)

            .AddSessionStateTempDataProvider();


        services.AddSingleton<IEmailSender, EmailSender>();

    }

应用程序用户.cs:


public class ApplicationUser : IdentityUser

{

    [Required]

    public string FirstName { get; set; }


    [Required]

    public string Name { get; set; }

}

索引.cshtml:


@model IEnumerable<TestIdentity.Models.ApplicationUser>

@{

    ViewData["Title"] = "Index";

}

<h2>Application users</h2>

@foreach (var item in Model)

{

    <p>@item.Name</p>

}


_context.ApplicationUsers.ToListAsync()尽管我的 AspNetUsers 表中有数据,但没有返回任何结果。


明月笑刀无情
浏览 163回答 2
2回答

侃侃尔雅

如果您想ApplicationUser为您的身份使用自定义而不是默认IdentityUser。您需要在声明模型后进行一些更改。1.在dbContext中:public class ApplicationDbContext : IdentityDbContext<ApplicationUser>2.在startup.cs中:services.AddIdentity<ApplicationUser,IdentityRole>(config => {&nbsp; &nbsp; &nbsp; &nbsp; config.SignIn.RequireConfirmedEmail = true;&nbsp; &nbsp; })...3.在你的 /Shared/_LoginPartial.cshtml@inject SignInManager<ApplicationUser> SignInManager@inject UserManager<ApplicationUser> UserManager

ABOUTYOU

IdentityUser不是您的dbContext. 你需要UserManager<IdentityUser>改用。你的ApplicationUsersController班级应该是这样的public class ApplicationUsersController : Controller{&nbsp; &nbsp; private readonly ApplicationDbContext _context;&nbsp; &nbsp; private readonly UserManager<IdentityUser> _userManager;&nbsp; &nbsp; public ApplicationUsersController(ApplicationDbContext context,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UserManager<IdentityUser> userManager;)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; _context = context;&nbsp; &nbsp; &nbsp; &nbsp; _userManager = userManager;&nbsp; &nbsp; }&nbsp; &nbsp; // GET: Administrator/ApplicationUsers&nbsp; &nbsp; public async Task<IActionResult> Index()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return View(await _userManager.Users.Cast<ApplicationUser>().ToListAsync());&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答