简体   繁体   English

无法按ID查找用户

[英]Can't Find User By ID

I am trying to create a "User Service" class where I Expose only the methods that would need to go through this. 我正在尝试创建一个“用户服务”类,其中我只公开了需要经历的方法。 One of this methods is FindById. 其中一种方法是FindById。 Here is my code: 这是我的代码:

using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security.DataProtection;

public interface IUserService : IDisposable
{
    Task<CustomUser> FindByIdAsync(string userId);
    ICustomUser FindById(string userId);
}

public class UserService : UserManager<CustomUser>, IUserService
{
    public UserService(IUserStore<CustomUser> store, IDataProtectionProvider dataProvider)
        : base(store)
    {
        UserValidator = new UserValidator<CustomUser>(this)
        {
            AllowOnlyAlphanumericUserNames = false,
            RequireUniqueEmail = true
        };

        PasswordValidator = new PasswordValidator
        {
            RequiredLength = 8,
            RequireNonLetterOrDigit = true,
            RequireDigit = true,
            RequireLowercase = true,
            RequireUppercase = true,
        };

        UserLockoutEnabledByDefault = true;
        DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(1);
        MaxFailedAccessAttemptsBeforeLockout = 5;

        var dataProtectionProvider = dataProvider;
        if (dataProtectionProvider != null)
        {
            UserTokenProvider =
                new DataProtectorTokenProvider<CustomUser>(dataProtectionProvider.Create("ASP.NET Identity"));
        }
    }

    public new async Task<ICustomUser> FindByIdAsync(string userId)
    {
        return await base.FindByIdAsync(userId);
    }

    public ICustomUser FindById(string userId)
    {
        return base.FindById(userId);
    }
}

The FindByIdAsync method works fine, however the FindById method won't even compile. FindByIdAsync方法工作正常,但FindById方法甚至不会编译。 My autocomplete suggests that the method is there, but when I type it out, it becomes red and says: 我的自动完成功能表明该方法存在,但是当我输入它时,它会变成红色并说:

Microsoft.AspNet.Identity.UserManager does not contain a definition for FindById Microsoft.AspNet.Identity.UserManager不包含FindById的定义

What am I doing wrong? 我究竟做错了什么?

EDIT: 编辑:

FindById comes from UserManagerExtensions (see https://msdn.microsoft.com/en-us/library/dn497471(v=vs.108).aspx ). FindById来自UserManagerExtensions(参见https://msdn.microsoft.com/en-us/library/dn497471(v=vs.108).aspx )。 Does this mean that I have to extend the extensions class somehow? 这是否意味着我必须以某种方式扩展扩展类? All I am really trying to do is allow the use of this method via my interface which is IUserService, but adding a reference there it forces me to implement a method, which I tried to do with little success as you can see above 我真正想做的就是允许通过我的接口IUserService使用这个方法,但是在那里添加一个引用它迫使我实现一个方法,我试图做的很少成功,如上所示

You can't call an extension method for base 你不能为base调用扩展方法

The reason for that seems to be that you can't pass base as a parameter, and that what an extension method is. 原因似乎是你不能将base作为参数传递,而且扩展方法是什么。

You can call the method in a static context like this, instead of as an extension. 您可以在这样的静态上下文中调用该方法,而不是作为扩展。

public ICustomUser FindById(string userId)
{
    return UserManagerExtentions.FindById(this, userId);
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM