Предполагая, что вы используете EF, вы должны быть в состоянии сделать что-то вроде этого:
public partial class tblMember : IUserSecret
{
public int id { get; set; }
public string membership_id { get; set; }
public string password { get; set; }
....other fields
/// <summary>
/// Username
/// </summary>
string UserName { get { return membership_id; set { membership_id = value; }
/// <summary>
/// Opaque string to validate the user, i.e. password
/// </summary>
string Secret { get { return password; } set { password = value; } }
}
В основном локальное хранилище паролей называется IUserSecretStore в новой системе. Вы должны быть в состоянии включить в вашем типе объекта в конструктор AccountController как и предполагается, что вы реализованы все правильно:
public AccountController()
{
var db = new IdentityDbContext<User, UserClaim, tblMember, UserLogin, Role, UserRole>();
StoreManager = new IdentityStoreManager(new IdentityStoreContext(db));
}
Примечания свойство пользователя будет содержать требования пользователя, и требование NameIdentifier сопоставляется с IUser.Id в системе Identity. Это напрямую не привязано к IUserSecret, который является просто магазином имени пользователя/пользователя. Система моделирует локальный пароль в качестве локального входа в систему с providerKey = имя пользователя и loginProvider = "Local"
Edit: Добавление пример заказного пользователя, а
public class CustomUser : User {
public string CustomProperty { get; set; }
}
public class CustomUserContext : IdentityStoreContext {
public CustomUserContext(DbContext db) : base(db) {
Users = new UserStore<CustomUser>(db);
}
}
[TestMethod]
public async Task IdentityStoreManagerWithCustomUserTest() {
var db = new IdentityDbContext<CustomUser, UserClaim, UserSecret, UserLogin, Role, UserRole>();
var manager = new IdentityStoreManager(new CustomUserContext(db));
var user = new CustomUser() { UserName = "Custom", CustomProperty = "Foo" };
string pwd = "password";
UnitTestHelper.IsSuccess(await manager.CreateLocalUserAsync(user, pwd));
Assert.IsTrue(await manager.ValidateLocalLoginAsync(user.UserName, pwd));
CustomUser fetch = await manager.Context.Users.FindAsync(user.Id) as CustomUser;
Assert.IsNotNull(fetch);
Assert.AreEqual("Custom", fetch.UserName);
Assert.AreEqual("Foo", fetch.CustomProperty);
}
EDIT # 2: Существует также ошибка в реализации IdentityAuthenticationmanager.GetUserClaims, которая отсылает пользователя вместо IUser, поэтому пользовательские пользователи, которые не распространяются на пользователя, не будут работать.
Вот код, который вы можете использовать для переопределения:
internal const string IdentityProviderClaimType = "http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider";
internal const string DefaultIdentityProviderClaimValue = "ASP.NET Identity";
/// <summary>
/// Return the claims for a user, which will contain the UserIdClaimType, UserNameClaimType, a claim representing each Role
/// and any claims specified in the UserClaims
/// </summary>
public override async Task<IList<Claim>> GetUserIdentityClaims(string userId, IEnumerable<Claim> claims) {
List<Claim> newClaims = new List<Claim>();
User user = await StoreManager.Context.Users.Find(userId) as IUser;
if (user != null) {
bool foundIdentityProviderClaim = false;
if (claims != null) {
// Strip out any existing name/nameid claims that may have already been set by external identities
foreach (var c in claims) {
if (!foundIdentityProviderClaim && c.Type == IdentityProviderClaimType) {
foundIdentityProviderClaim = true;
}
if (c.Type != ClaimTypes.Name &&
c.Type != ClaimTypes.NameIdentifier) {
newClaims.Add(c);
}
}
}
newClaims.Add(new Claim(UserIdClaimType, userId, ClaimValueTypes.String, ClaimsIssuer));
newClaims.Add(new Claim(UserNameClaimType, user.UserName, ClaimValueTypes.String, ClaimsIssuer));
if (!foundIdentityProviderClaim) {
newClaims.Add(new Claim(IdentityProviderClaimType, DefaultIdentityProviderClaimValue, ClaimValueTypes.String, ClaimsIssuer));
}
var roles = await StoreManager.Context.Roles.GetRolesForUser(userId);
foreach (string role in roles) {
newClaims.Add(new Claim(RoleClaimType, role, ClaimValueTypes.String, ClaimsIssuer));
}
IEnumerable<IUserClaim> userClaims = await StoreManager.Context.UserClaims.GetUserClaims(userId);
foreach (IUserClaim uc in userClaims) {
newClaims.Add(new Claim(uc.ClaimType, uc.ClaimValue, ClaimValueTypes.String, ClaimsIssuer));
}
}
return newClaims;
}
могли бы вы предоставить полный пример. Я бы хотел, чтобы мой собственный пользовательский класс сопоставлялся с таблицей «Мои пользователи». Похоже, я не единственный, кто ищет решение: http://stackoverflow.com/questions/17399933/how-do-i-configure-the-users-context-table –
Отредактировал мой ответ, чтобы добавить пользовательский Пользователь, вы, скорее всего, реализуете IUser вместо расширения User, но остальная часть кода должна быть одинаковой. –
@Hao Kung, я в настоящее время путаюсь, как правильно это реализовать, я понимаю, что это все еще в бета-версии, но там не возникает реальной информации о настройке и привязке к вашему собственному хранилищу баз данных и паролей, есть ли еще примеры/ресурсы, о которых вы знаете? – Tim