1
Identityを使用するC#でアプリケーションを作成しています。私もUserManagerの作成ApplicationUser IDの重要な負荷オブジェクト
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;
}
}
: は、私は自分のApplicationUser作成
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);
[ナビゲーションプロパティをロードするために強制EF ApplicationUser]を参照してください(https://stackoverflow.com/questions/32102708/forcing -ef-applicationuser-to-load-navigation-properties#) – bearing09