我在C#中创建一个使用Identity的应用程序。 我创建了自己的ApplicationUser:
public class ApplicationUser: IdentityUser
{
public string Naam { get; set; }
public string Zipcode { get; set; }
public bool Active { get; set; }
public UserRole HighestRole { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager, string authenticationType)
{
// 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;
}
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> 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;
}
}
我还创建了一个Usermanager:
public class UserManager : UserManager<Gebruiker>
{
public UserManager(IUserStore<Gebruiker> store)
: base(store)
{
}
public static UserManager Create(IdentityFactoryOptions<UserManager> options,IOwinContext context)
{
var manager = new UserManager(new UserStore<Gebruiker>(context.Get<ApplicationDbContext>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<Gebruiker>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider =
new DataProtectorTokenProvider<Gebruiker>(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
}
但是如果我想让用户离开我的数据库,则UserRole对象始终为null
这就是我如何让用户离开数据库。
ApplicationUser user = UserManager.FindById(correctId);
答案 0 :(得分:0)
public virtual UserRole HighestRole { get; set; }