简体   繁体   中英

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. 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. 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

What am I doing wrong?

EDIT:

FindById comes from UserManagerExtensions (see 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

You can't call an extension method for base

The reason for that seems to be that you can't pass base as a parameter, and that what an extension method is.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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