简体   繁体   English

与不同商店(UserStore,UserEmailStore,UserClaimStore,UserLockoutStore等)一起使用的UserManager

[英]UserManager used with different stores (UserStore, UserEmailStore, UserClaimStore, UserLockoutStore etc.)

I am trying to implement UserStore but I would like also to implement UserEmailStore and UserLockoutStore and the others. 我正在尝试实现UserStore,但我也想实现UserEmailStore和UserLockoutStore等。 As I noticed all User*Store are based on UserStore, no problem. 正如我注意到的那样,所有User * Store都基于UserStore,这没问题。 But I took a look into UserManager and I found, for me, strange thing. 但是我查看了UserManager之后,发现对我来说很奇怪。 You can inject several types of store to UserManager but always only one. 您可以将几种类型的商店注入到UserManager中,但始终只能注入一种。 But UserManager can works with all of them based on what type you injected. 但是UserManager可以根据您注入的类型来使用它们。

Fox example method GetLockoutEndDateAsync from UserManager 来自UserManager的Fox示例方法GetLockoutEndDateAsync

public virtual async Task<DateTimeOffset?> GetLockoutEndDateAsync(TUser user)
{
  this.ThrowIfDisposed();
  IUserLockoutStore<TUser> userLockoutStore = this.GetUserLockoutStore();
  if ((object) user == null)
    throw new ArgumentNullException("user");
  TUser user1 = user;
  CancellationToken cancellationToken = this.CancellationToken;
  return await userLockoutStore.GetLockoutEndDateAsync(user1, cancellationToken);
}

Method this.GetUserLockoutStore looks like this 方法this.GetUserLockoutStore看起来像这样

internal IUserLockoutStore<TUser> GetUserLockoutStore()
{
  IUserLockoutStore<TUser> userLockoutStore = this.Store as IUserLockoutStore<TUser>;
  if (userLockoutStore != null)
    return userLockoutStore;
  throw new NotSupportedException(Resources.StoreNotIUserLockoutStore);
}

There are other methods like 还有其他类似的方法

  • GetEmailStore GetEmailStore
  • GetPhoneNumberStore GetPhoneNumberStore
  • GetClaimStore GetClaimStore
  • GetLoginStore GetLoginStore
  • ... ...

So it means store must be based on correct interface you want to use. 因此,这意味着存储必须基于要使用的正确接口。

My question is, how to deal with this? 我的问题是,如何处理? Should I implement one store based on all possible User*Store interfaces? 是否应该基于所有可能的User * Store接口实现一个商店? Or can you suggest another solution? 还是可以建议其他解决方案?

Thanks in advance 提前致谢

Yes, implementing the required interfaces as "features" in one single store is the straightforward way to do it and it's also how ASP.NET Core Identity EF Core provider is implemented (see here ) 是的,在单个存储中将所需的接口实现为“功能”是实现此目的的直接方法,这也是实现ASP.NET Core Identity EF Core提供程序的方式(请参见此处

/// <summary>
/// Represents a new instance of a persistence store for the specified user and role types.
/// </summary>
/// <typeparam name="TUser">The type representing a user.</typeparam>
/// <typeparam name="TRole">The type representing a role.</typeparam>
/// <typeparam name="TContext">The type of the data context class used to access the store.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a role.</typeparam>
/// <typeparam name="TUserClaim">The type representing a claim.</typeparam>
/// <typeparam name="TUserRole">The type representing a user role.</typeparam>
/// <typeparam name="TUserLogin">The type representing a user external login.</typeparam>
/// <typeparam name="TUserToken">The type representing a user token.</typeparam>
/// <typeparam name="TRoleClaim">The type representing a role claim.</typeparam>
public abstract class UserStore<TUser, TRole, TContext, TKey, TUserClaim, TUserRole, TUserLogin, TUserToken, TRoleClaim> :
    IUserLoginStore<TUser>,
    IUserRoleStore<TUser>,
    IUserClaimStore<TUser>,
    IUserPasswordStore<TUser>,
    IUserSecurityStampStore<TUser>,
    IUserEmailStore<TUser>,
    IUserLockoutStore<TUser>,
    IUserPhoneNumberStore<TUser>,
    IQueryableUserStore<TUser>,
    IUserTwoFactorStore<TUser>,
    IUserAuthenticationTokenStore<TUser>
    where TUser : IdentityUser<TKey, TUserClaim, TUserRole, TUserLogin>
    where TRole : IdentityRole<TKey, TUserRole, TRoleClaim>
    where TContext : DbContext
    where TKey : IEquatable<TKey>
    where TUserClaim : IdentityUserClaim<TKey>
    where TUserRole : IdentityUserRole<TKey>
    where TUserLogin : IdentityUserLogin<TKey>
    where TUserToken : IdentityUserToken<TKey>
    where TRoleClaim : IdentityRoleClaim<TKey>
{
}

You only need to implement the interfaces you are going to support and leave the other ones out. 您只需要实现要支持的接口,而将其他接口排除在外。

If for some reason (Single Responsibility Principle) this is not possible (ie because you need to use a completely different type of database or some webservice or Active Directory for it), then you can implement individual stores and use a facade pattern to wrap around it and inject your individual stores into the facade and inject the facade. 如果由于某种原因(单一职责原则)无法这样做(即因为您需要使用完全不同类型的数据库或某种Web服务或Active Directory),则可以实现单个存储并使用外观模式进行包装并将您的个人商店注入立面并注入立面。

But it's more work and requires more setup of DI to do it. 但这需要更多的工作,并且需要更多的DI设置才能完成。 But doable. 但是可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ninject UserManager和UserStore - Ninject UserManager and UserStore 如何在UserManager和UserStore中使用DI - How to use DI with UserManager and UserStore 在AccountController之外放置UserManager和UserStore? - Disposing UserManager and UserStore outside of AccountController? UserManager.FindAsync不使用UserStore的自定义实现 - UserManager.FindAsync not working with custom implementation of UserStore 结合使用UserManager.FindAsync和自定义UserStore - Using UserManager.FindAsync with a custom UserStore 如何在 DI 中注册自定义 UserStore &amp; UserManager - How to register custom UserStore & UserManager in DI 如何使用用户管理器<identityuser>与用户存储<identityuser> ?</identityuser></identityuser> - How to use UserManager<IdentityUser> with UserStore<IdentityUser>? ASP.NET Identity 中 UserStore 和 UserManager 的使用有什么区别? - What is the difference in the use of UserStore and UserManager in ASP.NET Identity? 具有泛型类型的函数中使用的Ordeby(),Where()等 - Ordeby(), Where(), etc. used in a function with generic types 如何引用某些Windows元素(滚动条,推入选项按钮等)使用的BG颜色? - How to refer to BG-color, used by some Windows elements (scrollbar, pushed-in option button, etc.)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM