2016-04-26 67 views
1

我在使用身份的C#中创建应用程序。 我创建了自己ApplicationUser:在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的对象总是空

这是我如何让用户离开数据库。

ApplicationUser user = UserManager.FindById(correctId); 

CODE: enter image description here SAME TIME IN DATABASE: enter image description here

+0

参考[强制EF ApplicationUser要加载导航属性(https://stackoverflow.com/questions/32102708/forcing -ef-applicationuser到负载导航属性#) – bearing09

回答

0
public virtual UserRole HighestRole { get; set; }