简体   繁体   中英

Extend AspNetCore.Identity.EntityFrameworkCore.UserStore (V2.1.3)

I simply want to extend the userstore and add extra method. I'm not able to define the correct constructor the customuserstore. How do I define constructor

public class MyCustomUserStore<TUser> : Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore<TUser>, IUserLdapStore<TUser>
    where TUser : class
{
    // constructor goes here

    public Task<string> GetDistinguishedNameAsync(TUser user)
    {
        return Task.FromResult(string.Empty);
    }
}

public interface IUserLdapStore<TUser>
    where TUser : class
{
    /// <summary>
    /// When implemented in a derived class, gets the DN that should be used to attempt an LDAP bind for validatio of a user's password.
    /// </summary>
    /// <param name="user"></param>
    /// <returns></returns>
    Task<string> GetDistinguishedNameAsync(TUser user);
}

Getting following errors:

The type '`TUser' cannot be used as type parameter 'TUser' in the generic type or method 'UserStore'.

There is no implicit reference conversion from 'TUser' to 'Microsoft.AspNetCore.Identity.IdentityUser'.

'TUser' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TUser' in the generic type or method 'UserStore

There is no argument given that corresponds to the required formal parameter 'context' of 'UserStore.UserStore(DbContext, IdentityErrorDescriber)'

Going through the error message carefully you seem to be missing to add the constraint for TUser that forces it to inherit from IdentityUser<string> . This is an inherited constraint from Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore<TUser> .

Adding that constraint to your custom store is something like:

public class MyCustomUserStore<TUser> : Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore<TUser>, IUserLdapStore<TUser>
    where TUser : IdentityUser<string>, new()
{
    //constructor goes here....

    public Task<string> GetDistinguishedNameAsync(TUser user)
    {
        return Task.FromResult(string.Empty);
    }
}

Thanks to @KirkLarkin for pointing the second error I completely overlooked.

There is no argument given that corresponds to the required formal parameter 'context' of 'UserStore.UserStore(DbContext, IdentityErrorDescriber)'

The error is a little cryptic, but it's easily solvable if we provide a constructor for the custom store providing the parameters for the base class:

public MyCustomUserStore(DbContext context, IdentityErrorDescriber describer = null) : base(context, describer)
{
}

Putting all these together, the class should look like this:

public class MyCustomUserStore<TUser> : Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore<TUser>, IUserLdapStore<TUser>
    where TUser : IdentityUser<string>, new()
{
    public MyCustomUserStore(DbContext context, IdentityErrorDescriber describer = null) : base(context, describer)
    {
    }

    public Task<string> GetDistinguishedNameAsync(TUser user)
    {
        return Task.FromResult(string.Empty);
    }
}

Hope this helps!

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