[英]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.
我无法为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'.
'TUser'类型不能用作通用类型或方法'UserStore'中的类型参数'TUser'。
There is no implicit reference conversion from 'TUser' to 'Microsoft.AspNetCore.Identity.IdentityUser'.
没有从“ TUser”到“ 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
“ TUser”必须是具有公共无参数构造函数的非抽象类型,以便在通用类型或方法“ UserStore”中将其用作参数“ TUser”
There is no argument given that corresponds to the required formal parameter 'context' of 'UserStore.UserStore(DbContext, IdentityErrorDescriber)'
没有给出与“ UserStore.UserStore(DbContext,IdentityErrorDescriber)”的所需形式参数“ context”相对应的参数
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>
. 仔细检查错误消息,您似乎缺少为
TUser
添加约束的条件,该约束迫使TUser
从IdentityUser<string>
继承。 This is an inherited constraint from Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore<TUser>
. 这是从
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. 感谢@KirkLarkin指出了我完全忽略的第二个错误。
There is no argument given that corresponds to the required formal parameter 'context' of 'UserStore.UserStore(DbContext, IdentityErrorDescriber)'
没有给出与“ UserStore.UserStore(DbContext,IdentityErrorDescriber)”的所需形式参数“ context”相对应的参数
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! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.