簡體   English   中英

自定義Asp.Net Identity 3.0

[英]Customizing Asp.Net Identity 3.0

我想自定義Asp.Net Identity 3類。

這是我所做的:

public class MyDbContext : IdentityDbContext<User>

public class User : IdentityUser<int>

我還擴展了IdentityUserClaim<int>, IdentityRole<int>, IdentityUserLogin<int> and IdentityUserRole<int>但是出現以下錯誤:

The type 'User' cannot be used as type parameter 'TUser' in the generic type or method' IdentityDbContext<TUser>'.There is no implicit reference conversion from 'User' to 'Microsoft.AspNet.Identity.EntityFramework.IdentityUser'.

我不知道您是如何使您的類繼承IdentityUser<int>因為“ IdentityUser”的通用版本具有更多的類型參數。 在這里檢查: https : //msdn.microsoft.com/en-us/library/dn613256%28v=vs.108%29.aspx

因此,您將需要:

public class User : IdentityUser<int, UserLogin, UserRole, UserClaim>

和:

public class UserRole : IdentityUserRole<int> { }
public class UserClaim : IdentityUserClaim<int> { }
public class UserLogin : IdentityUserLogin<int> { }

編輯:對於身份3.0,事情有些不同,但是問題是相似的。 根據: https : //github.com/aspnet/Identity/blob/dev/src/Microsoft.AspNet.Identity.EntityFramework/IdentityDbContext.cs

IdentityDbContext<TUser>是這樣的:

public class IdentityDbContext<TUser> : IdentityDbContext<TUser, IdentityRole, string> where TUser : IdentityUser
{ }

重要的部分是where TUser : IdentityUser IdenityUser的where TUser : IdentityUser定義是:

public class IdentityUser : IdentityUser<string>
{ ... }

而且您的User類繼承了IdentityUser<int>因此它沒有隱式轉換為'IdentityUser',因為int / string不同。

解決方案是從IdentityDbContext<TUser, TRole, TKey>繼承IdentityDbContext<TUser, TRole, TKey>其中TUser是您的User類, TRole是繼承了IdentityRole<int>並且TKeyint新Role類:

public class MyDbContext : IdentityDbContext<User, Role, int> {...}
public class User : IdentityUser<int> {...}
public class Role : IdentityRole<int> {...}

這應該足夠了:

public class ApplicationUser : IdentityUser<int>
{

}

public class ApplicationRole : IdentityRole<int>
{

}

public class MyDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int>
{

}

是的,您可以自定義Identity中的默認類。 我在Identity Framework上創建了一個易於使用的包裝器,並使用其他屬性創建了自定義類。 如果您想看看,請訪問博客

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM