如何获得ASP.NET身份用户的列表? [英] How to obtain a list of Users from 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 things, making things easier.
有一个在GitHub上中的 Asp.NET身份示例项目。
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.
我使用的是新的<一个href=\"http://blogs.msdn.com/b/webdev/archive/2013/06/27/introducing-asp-net-identity-membership-system-for-asp-net-applications.aspx\">ASP.NET身份会员制,果然,创建用户和添加角色非常简单,直观。
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.)
修改:如果我试图做到这一点。
Edit: If I try to do this
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>
{
}
}
所以我的猜测是,鉴
列告诉分开 ApplicationUser
从用户
。然而,它并不在我的数据库中存在(这是由应用程序自动创建的。)
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
对象,所以我只是去进取,改变了这一切用途普通的旧式用户
。然后,我只是改变了 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();
不过,我认为这会回来困扰着我的未来(我可能会需要更多的字段添加到用户
),所以可能我得使用同样的方法在这个问题:
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的身份示例项目行比较的线,并且发现了生成的项目有以下行:
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身份用户的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!