0

我通过将我的实体搭建到数据库中(代码优先)构建了一个身份系统,构建了一个从 IUserStore 继承的 EmployeeStore,但是,每当我的 OnPost 方法尝试在 EmployeeStore 中使用 NormalizedUserName 方法时,我总是会收到对 UserManager 的回溯错误(请检查下面的错误)

发生异常:CLR/System.NullReferenceException Nextekk.dll 中发生“System.NullReferenceException”类型的异常,但未在用户代码中处理:“对象引用未设置为对象的实例。” 在 Nextekk.Models.EmployeeStore.SetNormalizedUserNameAsync(Employee employee, String normalizedName, CancellationToken cancelToken) in /home/Xxxxxx/Nxxxxxx/Models/EmployeeStore.cs:Microsoft.AspNetCore.Identity.UserManager`1.d__82.MoveNext() 的第 113 行

我什至尝试将 UserManager 注入到 EmployeeStore 的构造函数中并调用它以直接使用 UserManager,我真的不希望将其作为一个选项,但一切都是徒劳的。

我的 NormalizedUserName 方法如下所示:

public Task SetNormalizedUserNameAsync(Employee employee, string normalizedName,      CancellationToken cancellationToken)
    {
        cancellationToken.ThrowIfCancellationRequested();
        if (employee == null)
        {
            throw new ArgumentNullException(nameof(employee));
        }
        if (normalizedName == null)
        {
            throw new ArgumentNullException(nameof(normalizedName));
        }

        // base.SetNormalizedUserNameAsync(Employee employee, string normalizedName,     CancellationToken cancellationToken);

        employee.NormalizedUserName = normalizedName;
        // db.SaveChanges();
        // return Task.CompletedTask;
        return Task.FromResult<object>(null);
    }

我的启动身份服务如下所示:

public void ConfigureServices(IServiceCollection services)
    {


        services.AddDbContext<Models.NextekkDBContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("NDB")));
        // Add identity
        services.AddIdentity<Employee, Role>()
        .AddUserStore<EmployeeStore>()
        .AddRoleStore<RoleStore>()
        .AddEntityFrameworkStores<NextekkDBContext>()
        .AddDefaultTokenProviders()
        .AddClaimsPrincipalFactory<EmployeeClaimsPrincipalFactory>(); 


        services.AddScoped<EmployeeClaimsPrincipalFactory>();




        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);



    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {

        app.UseAuthentication();

请问我的问题是如何让我的 CustomUserStore (EmployeeStore) 与 Microsoft Identity 的默认 UserManager 一起工作。任何帮助将非常感激。

4

0 回答 0