我已经开始使用asp.net身份几天了,我正在尝试扩展和自定义身份
我创建了一个自定义类,它到目前为止一直在工作 这意味着我扩展了所有身份类:
public class User : IdentityUser<string, UserLogin, UserRole, UserClaim>, IUser, IUser<string>
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
你可以看到User类中的所有类都是我的类,
我为UserStore,RoleStore,UserManager,RoleManager做了那个:
public class HEUserManager : UserManager<User>
{
public HEUserManager(IUserStore<User> store)
: base(store)
{
}
public static HEUserManager Create(IdentityFactoryOptions<HEUserManager> options, IOwinContext context)
{
var manager = new HEUserManager(new HEUserStore(context.Get<HEDbContext>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<User>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
// Configure user lockout defaults
manager.UserLockoutEnabledByDefault = true;
manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
manager.MaxFailedAccessAttemptsBeforeLockout = 5;
// Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
// You can write your own provider and plug it in here.
manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<User>
{
MessageFormat = "Your security code is {0}"
});
manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<User>
{
Subject = "Security Code",
BodyFormat = "Your security code is {0}"
});
manager.EmailService = new EmailService();
manager.SmsService = new SmsService();
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider =
new DataProtectorTokenProvider<User>(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
}
// Configure the application sign-in manager which is used in this application.
public class HESignInManager : SignInManager<User, string>
{
public HESignInManager(HEUserManager userManager, IAuthenticationManager authenticationManager)
: base(userManager, authenticationManager)
{
}
public override Task<ClaimsIdentity> CreateUserIdentityAsync(User user)
{
return user.GenerateUserIdentityAsync((HEUserManager)UserManager);
}
public static HESignInManager Create(IdentityFactoryOptions<HESignInManager> options, IOwinContext context)
{
return new HESignInManager(context.GetUserManager<HEUserManager>(), context.Authentication);
}
}
public class HEUserStore : UserStore<User, Role, string, UserLogin, UserRole, UserClaim>, IUserStore<User>, IUserStore<User,string>, IDisposable
{
public HEUserStore(DbContext context) : base(context)
{
}
}
public class HERoleManager : RoleManager<Role, string>
{
public HERoleManager(IRoleStore<Role, string> store) : base(store)
{
}
}
public class HERoleStore : RoleStore<Role, string, UserRole>
{
public HERoleStore(DbContext context) : base(context)
{
}
}
它到目前为止工作,但在自定义之前,使用用户管理器创建用户非常简单,并在数据库中为我创建了一个用户
但是现在我必须每次都手动输入用户ID,否则我会收到此错误
An exception of type 'System.Data.Entity.Validation.DbEntityValidationException' occurred in mscorlib.dll but was not handled in user code
Additional information: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
这是正常的还是我做错了什么?如果这是正常的,我怎么能再次自动生成ID?