ASP에서 사용자 목록을 가져오는 방법NET 아이덴티티?
편집: 이 질문은 구식입니다.
이 질문을 하는 순간 아이덴티티 프레임워크는 감동적인 목표였습니다.저자들은 꽤 많은 것들을 바꿨고 다른 것들을 분리해서 모든 것을 쉽게 만들었습니다.
아스프를 보세요.깃허브의 NET 아이덴티티 샘플 프로젝트.
저는 사용자 관리가 필요한 작은 애플리케이션을 만들고 있습니다.등록은 허용되지 않으며 로그인 정보를 생성하고 수정하는 슈퍼 유저가 있습니다.
저는 새로운 ASP를 사용하고 있습니다.NET Identity 멤버십 시스템은 사용자를 생성하고 역할을 추가하는 것이 쉽고 직관적입니다.
자, 제 질문은.생성된 AccountController 클래스에서 사용하는 AuthenticationIdentityManager 클래스를 사용하여 사용자 목록을 가져오는 방법은 무엇입니까?컨트롤러에서 사용자 목록에 액세스하는 방법을 찾을 수 없습니다.
(그런데, "아이덴티티"라는 새로운 이름이 어떤 사람들에게는 멋지게 들릴지 모르지만, 그것은 찾기 힘든 고통입니다.)
편집: 이 작업을 시도하는 경우
ApplicationDbContext UsersContext = new ApplicationDbContext();
UsersContext.Users.ToList(); // Exception
예외가 있습니다.Invalid column name 'Discriminator'. ApplicationDbContext의 정의는 새 응용 프로그램 마법사에 의해 자동으로 생성됩니다.
using Microsoft.AspNet.Identity.EntityFramework;
namespace Cobranzas.Models
{
public class ApplicationUser : User
{
}
public class ApplicationDbContext : IdentityDbContextWithCustomUser<ApplicationUser>
{
}
}
그래서 제 추측으로는.Discriminator칼럼은 구분하기 위한 것입니다.ApplicationUser부터User. 그러나 응용프로그램이 자동으로 작성한 데이터베이스에는 존재하지 않습니다.
제가 디바이더를 사용하고 있지 않다는 걸 알게 됐어요ApplicationUser무엇이든 목적이 있어서, 그냥 진행해서 모든 용도를 일반적인 오래된 것으로 바꿨습니다.User. 그리고 난 그냥 변했어요.ApplicationDbContext다음에 대한 정의:
public class ApplicationDbContext : IdentityDbContext<
User, UserClaim, UserSecret, UserLogin,
Role, UserRole, Token, UserManagement>
{
}
이제 사용자 목록에 액세스할 수 있습니다.
UsersContext = new ApplicationDbContext();
...
UsersContext.Users.ToList();
하지만, 저는 이것이 미래에 다시 돌아와 저를 괴롭힐 것이라고 생각합니다. (저는 아마도 더 많은 분야를 추가해야 할 것입니다.)User) 그래서 아마 저는 이 질문과 같은 접근법을 사용해야 할 것입니다.
ASP에서 모든 역할 이름을 가져옵니다.NET MVC5 아이덴티티 시스템
편집: 새 속성을 추가해야 했기 때문에 변경 사항을 되돌려야 했습니다.그래서 ASP와 한 줄 한 줄 비교를 했습니다.NET Identity Sample Project(넷 아이덴티티 샘플 프로젝트)에서 생성된 프로젝트의 라인은 다음과 같습니다.
IdentityManager = new AuthenticationIdentityManager(new IdentityStore());
샘플 응용프로그램이 데이터베이스 컨텍스트를 생성자에 포함시킨 경우.그래서 나는 그것을 내 컨스트럭터에 추가했고, 데이터베이스를 다시 만들었고, 문제는 사라졌습니다.
IdentityManager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext()));
- 만들다
ASP .NET MVC5프로젝트 기본값 - 만들다
ASP .NET Identity테이블과 연결 문자열도 올바르게 변경합니다. - 사용자를 확보하기 위해서는 다음 A 테스트만 하면 됩니다.에 가다
AccountController에 B를 . 임의의 더미 방법을 만들어 거기에 놓습니다.
var context = new ApplicationDbContext(); var allUsers = context.Users.ToList();

RTM의 경우, 당신은 당신의 다음으로 내려가야 할 것입니다.DbContext또는 특정 저장소 구현이 모든 사용자를 열거해야 하는 경우.다음 릴리스에서는 옵션을 추가할 가능성이 높습니다.IQueryable저장소가 노출되도록 구현할 수 있는 Manager 클래스의 사용자/역할 메서드IQueryables사용자와 상점 모두에게 적용됩니다.
using System.Linq;
using System.Data;
using System.Data.Entity;
var db = new ApplicationDbContext();
var Users = db.Users.Include(u => u.Roles);
만약 우리가 다음의 타입을 사용할 수 있다면,Constructor인에Identity AccountController.
public AccountController(ApplicationUserManager userManager,
ISecureDataFormat<AuthenticationTicket> accessTokenFormat)
{
UserManager = userManager;
AccessTokenFormat = accessTokenFormat;
}
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
그럼 우리가 직접 사용할 수 있습니다.UserManager개체가 다음과 같이 사용자 목록을 가져옵니다.
var userList= UserManager.Users.ToList();
오른쪽 유형을 명시적으로 설정하여 이 작업을 수행할 수 있습니다.
var userStore = new UserStore<IdentityUser>();
var userManager = new UserManager<IdentityUser>(userStore);
IQueryable<IdentityUser> usersQuery = userManager.Users;
List<IdentityUser> users = usersQuery.ToList();
Imports:
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System.Linq;
using System.Collections.Generic;
언급URL : https://stackoverflow.com/questions/18773838/how-to-obtain-a-list-of-users-from-asp-net-identity
'programing' 카테고리의 다른 글
| 다단계 도커 파일의 공유 변수: 대체되지 않은 FROM 이전 ARG (0) | 2023.10.28 |
|---|---|
| 순서 없는 목록 항목에서 들여쓰기를 제거하는 방법은 무엇입니까? (0) | 2023.10.28 |
| Oracle 데이터베이스의 다른 스키마에 있는 테이블 이름 목록 가져오기 (0) | 2023.10.28 |
| jQuery를 사용하는 클래스와 가장 가까운 이전 형제를 어떻게 찾을 수 있습니까? (0) | 2023.10.28 |
| 해시는 파이썬에서 무엇을 합니까? (0) | 2023.10.28 |