2014-11-14 18 views
4

我正在開發一個帶有.NET Framework 4.5.1和Asp.Net Identity 2.1.0的Web Api 2.2應用程序。從IdentityUser繼承我在UserManager上遇到錯誤<User>

我不知道我在做什麼,但我想我的合併與ASP.NET身份數據庫的數據庫,我已經做到了這一點:

我自己dbContext

public class EFDbContext : IdentityDbContext, IUnitOfWork 

我自己的User類。

public class User : 
    IdentityUser<long, 
       IdentityUserLogin<long>, 
       IdentityUserRole<long>, 
       IdentityUserClaim<long> 
       > 

但是,當我這樣做:

UserManager<User> _userManager;

我得到這個錯誤:

類型Data.Models.User不能用作TUser類型的參數。從Data.Models.UserMicrosoft.AspNet.Identity.IUser<string>沒有任何明確的轉換。

我這樣做是因爲我想要IdentityUser.Idlong而不是string

我該如何解決這個錯誤?

UserManager<User, long> _userManager; 

我在這裏得到了三個錯誤:

EFDbContext_ctx = context as EFDbContext; 
_userManager = new UserManager<User, long>(new UserStore<User>(_ctx)); 
  1. The best match of method overload for 'Microsoft.AspNet.Identity.UserManager.UserManager (Microsoft.AspNet.Identity.IUserStore)' has some invalid arguments-
  2. The type 'Data.Models.User' cannot be used as parameter of type 'TUser' type or generic method 'Microsoft.AspNet.Identity.EntityFramework.UserStore '. There is no conversion from implicit reference from 'Data.Models.User' to 'Microsoft.AspNet.Identity.EntityFramework.IdentityUser'.
  3. Argument 1: cannot be converted from 'Microsoft.AspNet.Identity.EntityFramework.UserStore' to 'Microsoft.AspNet.Identity.IUserStore'

我怎樣才能解決這個新的錯誤

UPDATE

與更新的UserManager後?

+0

我有類似的東西。你是否通過'UserManager'的實例化修正了最後一個錯誤?請參閱我的問題http://stackoverflow.com/questions/31024859/how-do-i-expose-usermanageriapplicationuser-to-my-business-layer-while-hidin – Artyom 2015-06-24 11:00:32

回答

5

使用other UserManager

UserManager<User, long> _userManager; 

您正在使用this UserManager其中:

Represents the user manager for users where the primary key for the user is of type string.

+0

感謝您的回答。現在,我在這裏有同樣的問題:'_userManager = new UserManager (new UserStore (_ctx));'。 – VansFannel 2014-11-14 16:46:57

+0

您的用戶存儲應該將您的上下文作爲其類型參數 – Jonesopolis 2014-11-14 16:48:40

+0

我已更新我的問題,並提供了更多詳細信息。 – VansFannel 2014-11-14 16:51:34

1

當你用自己的用戶類擴展,你必須提供身份使用類型所有

這裏是我的上下文類,請注意,我用字符串作爲鍵:

public class EarlyDetectionContext 
    : IdentityDbContext<User, Role, string, EDLogin, RoleUserAssociation, EDClaim> 

當然,你也需要爲角色用戶登陸,userclaim和班級的UserRole中。 這裏是我的:

public class EDClaim : IdentityUserClaim<string> 
{ 
} 
public class EDLogin : IdentityUserLogin<string> 
{ 
} 
[Table("Roles", Schema = "ED")] 
public class Role : IdentityRole<string, RoleUserAssociation> 
{ 
    [Required, Column("DisplayName")] 
    public string DisplayName { get; set; } 

    [Required, Column("Created")] 
    public DateTime Created { get; set; } 

    [Required, Column("Updated")] 
    public DateTime Updated { get; set; } 

    [Required, Column("Deleted")] 
    public bool Deleted { get; set; } 
} 
[Table("RoleUserAssociations", Schema = "ED")] 
public class RoleUserAssociation : IdentityUserRole<string> 
{ 
    // 
    // Due to Identity 2 the Id needs to of string type 
    // 
    [Key] 
    [Required] 
    public string ID { get; set; } 

    //[Required, Column("User_Id")] 
    //public User User { get; set; } 

    //[Required, Column("Role_Id")] 
    //public Role Role { get; set; } 

    [Required, Column("Created")] 
    public DateTime Created { get; set; } 

    [Required, Column("Updated")] 
    public DateTime Updated { get; set; } 

    [Required, Column("Deleted")] 
    public bool Deleted { get; set; } 
} 
[Table("Users", Schema = "ED")] 
public class User : IdentityUser<string, EDLogin, RoleUserAssociation, EDClaim> 
{ 
    // This is needed for the new Identity 2 framework 
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User, string> 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; 
    } 

    [Required, Column("Municipality_Id")] 
    public Municipality Municipality { get; set; } 

    public string PasswordSalt { get; set; } 

    [Required, Column("FirstName")] 
    public string FirstName { get; set; } 

    [Required, Column("LastName")] 
    public string LastName { get; set; } 

    [Column("Title")] 
    public string Title { get; set; } 

    [Required, Column("Created")] 
    public DateTime Created { get; set; } 

    [Required, Column("Updated")] 
    public DateTime Updated { get; set; } 

    [Required, Column("Deleted")] 
    public bool Deleted { get; set; } 

    public bool Complete { get; set; } 

    [Required] 
    public DateTime Activated { get; set; } 
} 

你需要在每類中指定的密鑰類型,在我的情況的字符串,你的情況很長。 另請注意,我的聲明和登錄類是空的。沒關係,我不必在我的情況下添加任何功能或屬性,但我確實需要指定它們。

並且需要User類中的GenerateUserIdentityAsync方法。

在Startup.Auth.cs文件,你需要添加:

// Configure the db context, user manager and signin manager to use a single instance per request 
     app.CreatePerOwinContext(EarlyDetectionContext.Create); 
     app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); 
     app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create); 

您還需要OWIN啓動類(可以通過右鍵單擊添加 - 添加 - 新項)和Identity.cs文件。

如果您可以閱讀德語,請看看這blog
它基本上描述了你想要做的事情。 (儘管db-first方法)
另外看他的video(他會說英語很好)。
part 2也是。

我希望這給你足夠的信息前進:)