2016-04-26 80 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; }