如何从 ASP.NET Identity 获取用户列表? [英] How to obtain a list of Users from ASP.NET Identity?

查看:49
本文介绍了如何从 ASP.NET Identity 获取用户列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题已经过时

在我提出这个问题时,身份框架是一个不断变化的目标.作者改变了很多东西,他们已经解耦了其他一些东西,让一切变得更容易.

The Identity Framework was a moving target at the moment I asked this. The authors changed quite a few things and they have decoupled several others, making everything easier.

查看 github 上的 Asp.NET Identity Sample 项目.

Have a look at the Asp.NET Identity Sample project on github.

我正在创建一个需要用户管理的小型应用程序.不允许注册,而是有一个超级用户来创建和修改登录信息.

I'm creating a small application that requires user management. Registration is not allowed, instead there is a super user that will create and modify login information.

我正在使用新的 ASP.NET Identity 会员系统,果然,创建用户和添加角色既简单又直观.

I'm using the new ASP.NET Identity membership system, and sure enough, creating users and adding roles is easy and intuitive.

现在,我的问题是:如何使用生成的 AccountController 类使用的 AuthenticationIdentityManager 类获取用户列表?我找不到从控制器访问用户列表的方法.

Now, my question: How to obtain a list of users using the AuthenticationIdentityManager class that is used by the generated AccountController class? I couldn't find a way to access the user list from my controller.

(顺便说一句,新名称身份"对某些人来说可能听起来很棒,但搜索起来很痛苦.)

(By the way, the new name "Identity" may sound awesome to some people but it is a pain to search for.)

编辑:如果我尝试这样做

ApplicationDbContext UsersContext = new ApplicationDbContext();
UsersContext.Users.ToList(); // Exception

我收到一个异常无效的列名鉴别器".ApplicationDbContext的定义由新建应用向导自动生成:

I get an exception Invalid column name 'Discriminator'. The definition of ApplicationDbContext is generated automatically by the new application wizard:

using Microsoft.AspNet.Identity.EntityFramework;

namespace Cobranzas.Models
{
    public class ApplicationUser : User
    {

    }
    public class ApplicationDbContext : IdentityDbContextWithCustomUser<ApplicationUser>
    {

    }
}

所以我的猜测是 Discriminator 列用于区分 ApplicationUserUser.但是,它不存在于我的数据库中(由应用程序自动创建.)

So my guess is that Discriminator column is for telling apart ApplicationUser from User. However, it does not exists in my database (which was created automatically by the application.)

推荐答案

我发现我没有将派生的 ApplicationUser 对象用于任何事情,所以我继续更改了它用于普通的旧 User.然后我只是更改了以下内容的 ApplicationDbContext 定义:

I found out that I wasn't using the derived ApplicationUser object for anything, so I just went ahead and changed all uses of it for plain old User. Then I just changed ApplicationDbContext definition for the following:

public class ApplicationDbContext : IdentityDbContext<
    User, UserClaim, UserSecret, UserLogin,
    Role, UserRole, Token, UserManagement>
{
}

现在我可以访问用户列表了:

And now I can access the user list:

UsersContext = new ApplicationDbContext();
...
UsersContext.Users.ToList();

但是,我认为这会在将来再次困扰我(我可能需要向 User 添加更多字段)所以可能我将不得不使用与这个问题:

However, I think this will come back and haunt me in the future (I'll probably need to add more fields to User) so probably I'll have to use the same approach as in this question:

获取 ASP.NET MVC5 身份系统中的所有角色名称

编辑:因为我需要添加一个新属性,所以我不得不恢复我的更改.于是我继续和ASP.NET Identity Sample Project逐行对比,发现生成的项目有如下一行:

Edit: Since I got the need to add a new property, I had to revert my changes. So I went ahead and did a line by line comparison with the ASP.NET Identity Sample Project, and found out that the generated project had the following line:

IdentityManager = new AuthenticationIdentityManager(new IdentityStore());

而示例应用程序在构造函数中包含了数据库上下文.所以我将它添加到我的构造函数中,重新创建了数据库,问题就消失了.

while the Sample application had included the database context in the constructor. So I added it in my constructor, recreated the database and the problem went away.

IdentityManager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext()));

这篇关于如何从 ASP.NET Identity 获取用户列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆