C# 使用aspnet标识的附加验证
我正在使用AsNetIdentity进行附加验证,但它不起作用,这会导致此错误。这是我的界面代码C# 使用aspnet标识的附加验证,c#,asp.net-identity,C#,Asp.net Identity,我正在使用AsNetIdentity进行附加验证,但它不起作用,这会导致此错误。这是我的界面代码 using Health.BLL.CustomModels.UserModels; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.EntityFramework; using System.Collections.Generic; using System.Threading.Tasks; namespace He
using Health.BLL.CustomModels.UserModels;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Health.BLL.AuthRepository
{
public interface IAuthRepository
{
Task<IdentityResult> RegisterUser(UserModel userModel);
Task<IdentityUser> FindUser(string userName, string password);
Task<IList<string>> GetRolesAsync(string userid);
Task<IdentityResult> ValidateAsync<TUser>(UserManager<TUser> manager, TUser user) where TUser : class;
}
}
使用Health.BLL.CustomModels.UserModels;
使用Microsoft.AspNet.Identity;
使用Microsoft.AspNet.Identity.EntityFramework;
使用System.Collections.Generic;
使用System.Threading.Tasks;
命名空间Health.BLL.AuthRepository
{
公共接口IAuthRepository
{
任务注册服务器(UserModel UserModel);
任务FindUser(字符串用户名、字符串密码);
任务GetRolesAsync(字符串用户ID);
taskvalidateAsync(UserManager,TUser用户),其中TUser:class;
}
}
有关错误参考,请参见下图
类型“TUser”不能用作中的类型参数“TUser”
泛型类型或方法“UserManager”。没有隐含的
从“TUser”到
“Microsoft.AspNet.Identity.IUser”
穆罕默德,也许你使用的Nuget软件包有问题 我试图重现你的错误,但一切都很好!我在一个使用Asp.NETCore2.2的项目和另一个使用Asp.NETCore3.0/3.1的项目中测试了您的代码 对于Asp.Net Core 2.2中的项目,我安装了Nuget软件包Microsoft.AspNet.Identity.EntityFrameworkCore 2.2.3版 对于Asp.NETCore3.0/3.1中的项目,我安装了相同的Nuget软件包,但版本为3.1.3 以及我用来测试的代码:
using Microsoft.AspNetCore.Identity;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace WebApplication1
{
public interface IAuthRepository
{
Task<IdentityUser> FindUser(string userName, string password);
Task<IList<string>> GetRolesAsync(string userid);
Task<IdentityResult> ValidateAsync<TUser>(UserManager<TUser> manager, TUser user) where TUser : class;
}
}
使用Microsoft.AspNetCore.Identity;
使用System.Collections.Generic;
使用System.Threading.Tasks;
命名空间WebApplication1
{
公共接口IAuthRepository
{
任务FindUser(字符串用户名、字符串密码);
任务GetRolesAsync(字符串用户ID);
taskvalidateAsync(UserManager,TUser用户),其中TUser:class;
}
}
也许您应该使用您在项目中使用的Asp.Net Framework版本以及您正在使用的标识的Nuget包来编辑您的问题 传递给UserManager的泛型类型需要实现接口
IUser
,其中TKey是用户的密钥类型,可以是string、int等
因此,假设您的用户密钥类型(TKey)为string,将ValidateAsync的约定更改为:
Task<IdentityResult> ValidateAsync<TUser>(UserManager<TUser> manager, TUser user)
where TUser : class, IUser<string>;
Task ValidateAsync(用户管理器,TUser用户)
其中TUser:class,IUser;
有关更多详细信息,请参阅文档:
试试看,让我知道它是否有效。你需要查看
UserManager
的定义,并将TUser
的约束复制到TUser
的方法定义约束中。请将错误消息添加为文本而不是屏幕截图。我明白了。我没有使用aspnet身份核心。我使用的是使用Microsoft.aspnet.identity.EntityFramework 2.0的简单aspnet标识框架。如果没有aspnet identity core,我如何使用它来实现这一点?