UserManager用于不同的商店(UserStore,UserEmailStore,UserClaimStore,UserLockoutStore等)

时间:2017-02-16 11:00:34

标签: c# asp.net-core .net-core usermanager

我正在尝试实现UserStore,但我还想实现UserEmailStore和UserLockoutStore以及其他。我注意到所有User * Store都基于UserStore,没问题。但我看了一下UserManager,我发现,对我来说,奇怪的是。您可以向UserManager注入多种类型的商店,但始终只有一种。但是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);
}

方法 this.GetUserLockoutStore 看起来像这样

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

还有其他方法,如

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

因此,这意味着商店必须基于您想要使用的正确界面。

我的问题是,如何处理这个问题?我应该基于所有可能的User * Store界面实现一个商店吗?或者你能提出另一种解决方案吗?

提前致谢

1 个答案:

答案 0 :(得分:2)

是的,在一个商店中将所需的接口实现为“功能”是一种直接的方式,它也是实现ASP.NET核心标识EF核心提供程序的方式(参见here

250-smtp.gmail.com at your service, [108.167.189.105] 250-SIZE 35882577 250-8BITMIME 250-STARTTLS 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHUNKING 250 SMTPUTF8 
2017-02-16 07:38:56 CLIENT -> SERVER: STARTTLS 
2017-02-16 07:38:56 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS 
2017-02-16 07:38:56 CLIENT -> SERVER: EHLO www.pwebk.com 
2017-02-16 07:38:56 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [108.167.189.105] 250-SIZE 35882577 250-8BITMIME 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHUNKING 250 SMTPUTF8 
2017-02-16 07:38:56 CLIENT -> SERVER: AUTH LOGIN 
2017-02-16 07:38:56 SERVER -> CLIENT: 334 VXNlcm5hbWU6 
2017-02-16 07:38:56 CLIENT -> SERVER: bW5kdW5ndTkxQGdtYWlsLmNvbQ== 
2017-02-16 07:38:56 SERVER -> CLIENT: 334 UGFzc3dvcmQ6 
2017-02-16 07:38:56 CLIENT -> SERVER: bWFydGlubm4= 
2017-02-16 07:38:57 SERVER -> CLIENT: 534-5.7.14 Please log in via your web browser and 534-5.7.14 then try again. 534-5.7.14 Learn more at 534 5.7.14 https://support.google.com/mail/answer/78754 c192sm2741709oih.39 - gsmtp 
2017-02-16 07:38:57 SMTP ERROR: Password command failed: 534-5.7.14 Please log in via your web browser and 534-5.7.14 then try again. 534-5.7.14 Learn more at 534 5.7.14 https://support.google.com/mail/answer/78754 c192sm2741709oih.39 - gsmtp 
2017-02-16 07:38:57 SMTP Error: Could not authenticate. 
2017-02-16 07:38:57 CLIENT -> SERVER: QUIT 
2017-02-16 07:38:57 SERVER -> CLIENT: 221 2.0.0 closing connection c192sm2741709oih.39 - gsmtp 
2017-02-16 07:38:57 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Form could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

您只需要实现您要支持的接口,并将其他接口保留下来。

如果由于某种原因(单一责任原则)这是不可能的(因为您需要使用完全不同类型的数据库或某些Web服务或Active Directory),那么您可以实现单个商店并使用外观模式将它包裹起来并将你的各个商店注入立面并注入立面。

但是它的工作量更大,需要更多的DI设置才能完成。但可行。