【问题标题】:Custom UserStore in Asp .Net 5 beta 7Asp .Net 5 beta 7 中的自定义用户存储
【发布时间】:2015-09-27 20:57:56
【问题描述】:

我正在尝试根据over here 提出的想法在Asp .Net 5 beta 7 中实现自定义UserStore

这是我的自定义用户商店的代码:

public class ApplicationUserStore : UserStore<ApplicationUser>
{
    public ApplicationUserStore(DbContext context)
        : base(context)
    {
    }


    public async override Task<ApplicationUser> FindByIdAsync(string userId)
    {
        var user = await base.FindByIdAsync(userId);
        this.Context.Entry(user).Reference(u => u.Subject).Load();
        return user;
    }
}

我在这段代码中遇到了两个问题。

  1. 在基类中没有要覆盖的方法 FindByIdAsync
  2. FindByIdAsync 内部,找不到方法 Reference

我也不确定在编译后如何配置自定义用户存储。

更新:

我通过将 FindByIdAsync 覆盖的定义更改为以下解决了第一个问题:

public async override Task<ApplicationUser> FindByIdAsync(string userId, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))

第二个问题仍然是一个问题。如何在EF7 中指定我希望显式加载我的ApplicationUser 实体的引用属性Subject?在 EF6 命名空间 System.Data.EntityFramework.Infrastructure 中定义了以下内容:

public DbReferenceEntry<TEntity, TProperty> Reference<TProperty>(Expression<Func<TEntity, TProperty>> navigationProperty) where TProperty : class;

EF7 中定义的这个扩展方法在哪里?或者有没有一种新的方法来显式加载引用?

【问题讨论】:

  • 等一个星期(更多...),他们将再次更改所有结构,它将是 Identity v.5,并允许您配置您的身份验证,仅创建 20 个类并覆盖 80 个方法...(啊,别忘了接口,它有一千多个)。最好的是,这一次它会被记录在案。

标签: asp.net-identity asp.net-core


【解决方案1】:

你提到的那个问题已经很老了,从那时起,身份发生了很大的变化。

我可以告诉你我已经在我的项目中实现了 UserStore 而没有使用实体框架,我的类是这样声明的:

public sealed class UserStore<TUser> :
    IUserStore<TUser>,
    IUserPasswordStore<TUser>,
    IUserEmailStore<TUser>,
    IUserLoginStore<TUser>,
    IUserRoleStore<TUser>,
    IUserClaimStore<TUser>,
    IUserPhoneNumberStore<TUser>,
    IUserLockoutStore<TUser>,
    IUserTwoFactorStore<TUser>
   where TUser : SiteUser
{
.. implementation
}

SiteUser 是我自己的自定义类

您还可以找到 this question helpful,因为它显示了我如何在 DI 中插入自定义用户存储

看起来您确实想使用 EF,但如何声明和插入自定义用户存储的原则应该是相同的,无论是否依赖 EF

【讨论】:

  • 感谢乔提供的信息。我认为您链接的问题将有助于在编译后配置用户存储。是的,我正在使用实体框架。我本质上是在尝试解决我链接到的问题中的相同问题;即 FindByIdAsync 返回的用户对象没有加载引用属性。
猜你喜欢
  • 1970-01-01
  • 2014-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
相关资源
最近更新 更多