Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net core .net核心标识,不含使用int-id的EntityFramework_.net Core_Identity - Fatal编程技术网

.net core .net核心标识,不含使用int-id的EntityFramework

.net core .net核心标识,不含使用int-id的EntityFramework,.net-core,identity,.net Core,Identity,我正在尝试使用Identity w/o EntityFramework创建一个.net核心解决方案,但使用整数ID(UserId和RoleId)。我已经按照这个基本设置创建了解决方案并删除了EF(),但是它仍然为用户提供了愚蠢的Guid id列。当我试图更改它时,我在创建UserStore时遇到了一个问题,因为它继承自IUserStore,这要求我实现它 Task FindByIdAsync(字符串userId,CancellationToken CancellationToken) 表示Id值

我正在尝试使用Identity w/o EntityFramework创建一个.net核心解决方案,但使用整数ID(UserId和RoleId)。我已经按照这个基本设置创建了解决方案并删除了EF(),但是它仍然为用户提供了愚蠢的Guid id列。当我试图更改它时,我在创建
UserStore
时遇到了一个问题,因为它继承自
IUserStore
,这要求我实现它

Task FindByIdAsync(字符串userId,CancellationToken CancellationToken)

表示Id值是字符串而不是int

我读过几篇关于如何将基于EF的解决方案更改为使用ints的文章,我深入了解了EF是如何做到这一点的,但都是基于使用
TKey
的EF类

我试图创建自己的IUserStore,将字符串更改为int,但项目甚至无法启动(它可以正常编译,但当我尝试在本地运行它时,它会在完成启动之前退出)

有人知道如何做到这一点吗?我找了不少,什么也找不到

编辑: 到目前为止,我所做的是创建我自己的UserStore和RollStore(基本上只是原始版本的副本,一旦开始工作,我将对其进行调整)。在我的HomeController中,我添加了以下内容:

public class HomeController : Controller
{
    private readonly UserManager<models.User> _userManager;
    public HomeController(UserManager<models.User> userManager)
    {
      _userManager = userManager;
    }

    public async Task<IActionResult> Index()
    {
        var user = new models.User();
        if(User.Identity.IsAuthenticated)
            user = await _userManager.FindByNameAsync(User.Identity.Name);

        return View();
    }

}
这实际上是可行的(通过我的端点进行调试,
用户
已正确填充)。但是,如果仔细观察,我会发现
IUserStore
IRoleStore
Microsoft.AspNetCore.Identity
版本,这意味着我仍然依赖于这些合同,并且需要使用Guid作为Id

如果我强迫这个问题,使用

 services.AddSingleton<SandboxCoreDapper.Identity.Interfaces.IUserStore<User>, UserStore>();
 services.AddSingleton<SandboxCoreDapper.Identity.Interfaces.IRoleStore<Role>, RoleStore>();
同样地,对于
角色管理器

然后我添加到
Startup.cs

services.AddIdentity<User, Role>()
     .AddDefaultTokenProviders()
     .AddUserManager<SandboxCoreDapper.Identity.Services.UserManager<User>>()
     .AddRoleManager<SandboxCoreDapper.Identity.Services.RoleManager<Role>>()
            ;
services.AddIdentity()
.AddDefaultTokenProviders()
.AddUserManager()
.AddRoleManager()
;

到目前为止还不错……

这是对我的评论和你的问题/评论的回应:

只需声明您自己的UserStore,然后在Startup.cs中注册它

下面是我的UserStore实现,以适应此场景:

public class UserStore : IUserPasswordStore<Login>, IUserEmailStore<Login>, IUserPhoneNumberStore<Login>
    {
        private readonly IAccountRepo _accountRepo;

        public UserStore(IAccountRepo accountRepo)
        {
            _accountRepo = accountRepo;
        }

        public void Dispose()
        {
        }

        public async Task<string> GetUserIdAsync(Login user, CancellationToken cancellationToken)
        {
            return await Task.FromResult(user.Username);
        }

        public async Task<string> GetUserNameAsync(Login user, CancellationToken cancellationToken)
        {
            return await Task.FromResult(user.Username);
        }

        public Task SetUserNameAsync(Login user, string userName, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public Task<string> GetNormalizedUserNameAsync(Login user, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public async Task SetNormalizedUserNameAsync(Login user, string normalizedName, CancellationToken cancellationToken)
        {
            await Task.FromResult(user.Username = normalizedName.ToLower());
        }

        public async Task<IdentityResult> CreateAsync(Login user, CancellationToken cancellationToken)
        {
            await _accountRepo.CreateLogin(user);
            return IdentityResult.Success;
        }

        public async Task<IdentityResult> UpdateAsync(Login user, CancellationToken cancellationToken)
        {
            await _accountRepo.UpdateLogin(user.LoginId, user.Email, true);
            return IdentityResult.Success;
        }

        public Task<IdentityResult> DeleteAsync(Login user, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public async Task<Login> FindByIdAsync(string userId, CancellationToken cancellationToken)
        {
            return await _accountRepo.GetUser(userId);
        }

        public async Task<Login> FindByNameAsync(string normalizedUserName, CancellationToken cancellationToken)
        {
            return await _accountRepo.GetUser(normalizedUserName);
        }

        public async Task SetPasswordHashAsync(Login user, string passwordHash, CancellationToken cancellationToken)
        {
            if (user.LoginId != 0)
            {
                await _accountRepo.ChangePassword(user.Email, user.Username, passwordHash);
            }

            user.PasswordHash = passwordHash;
        }

        public async Task<string> GetPasswordHashAsync(Login user, CancellationToken cancellationToken)
        {
            return await Task.FromResult(user.PasswordHash);
        }

        public async Task<bool> HasPasswordAsync(Login user, CancellationToken cancellationToken)
        {
            return await Task.FromResult(!string.IsNullOrEmpty(user.PasswordHash) && !string.IsNullOrEmpty(user.Salt));
        }

        public Task SetEmailAsync(Login user, string email, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public Task<string> GetEmailAsync(Login user, CancellationToken cancellationToken)
        {
            return Task.FromResult(user.Email);
        }

        public async Task<bool> GetEmailConfirmedAsync(Login user, CancellationToken cancellationToken)
        {
            return await Task.FromResult(user.EmailConfirmed);
        }

        public Task SetEmailConfirmedAsync(Login user, bool confirmed, CancellationToken cancellationToken)
        {
            return Task.FromResult(user.EmailConfirmed = confirmed);
        }

        public Task<Login> FindByEmailAsync(string normalizedEmail, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public Task<string> GetNormalizedEmailAsync(Login user, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public Task SetNormalizedEmailAsync(Login user, string normalizedEmail, CancellationToken cancellationToken)
        {
            return Task.FromResult(user.Email = normalizedEmail.ToLower());
        }

        public Task SetPhoneNumberAsync(Login user, string phoneNumber, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public Task<string> GetPhoneNumberAsync(Login user, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public Task<bool> GetPhoneNumberConfirmedAsync(Login user, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public Task SetPhoneNumberConfirmedAsync(Login user, bool confirmed, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }
    }
公共类用户存储:IUserPasswordStore、IUserEmailStore、IUserPhoneNumberStore
{
私人只读IAccountRepo(u accountRepo);
公共用户存储(IAccountRepo accountRepo)
{
_accountRepo=accountRepo;
}
公共空间处置()
{
}
公共异步任务GetUserIdAsync(登录用户,CancellationToken CancellationToken)
{
返回wait Task.FromResult(user.Username);
}
公共异步任务GetUserNameAsync(登录用户,CancellationToken CancellationToken)
{
返回wait Task.FromResult(user.Username);
}
公共任务SetUserNameAsync(登录用户、字符串用户名、CancellationToken CancellationToken)
{
抛出新的NotImplementedException();
}
公共任务GetNormalizedUserNameAsync(登录用户,CancellationToken CancellationToken)
{
抛出新的NotImplementedException();
}
公共异步任务SetNormalizedUserNameAsync(登录用户、字符串normalizedName、CancellationToken CancellationToken)
{
等待Task.FromResult(user.Username=normalizedName.ToLower());
}
公共异步任务CreateAsync(登录用户,CancellationToken CancellationToken)
{
wait_accountRepo.CreateLogin(用户);
返回IdentityResult.Success;
}
公共异步任务UpdateAsync(登录用户,CancellationToken CancellationToken)
{
wait_accountRepo.UpdateLogin(user.LoginId,user.Email,true);
返回IdentityResult.Success;
}
公共任务DeleteAsync(登录用户,CancellationToken CancellationToken)
{
抛出新的NotImplementedException();
}
公共异步任务FindByIdAsync(字符串用户ID,CancellationToken CancellationToken)
{
返回wait_accountRepo.GetUser(userId);
}
公共异步任务FindByNameAsync(字符串normalizedUserName,CancellationToken CancellationToken)
{
return wait_accountRepo.GetUser(normalizedUserName);
}
公共异步任务SetPasswordHashAsync(登录用户、字符串passwordHash、CancellationToken CancellationToken)
{
如果(user.LoginId!=0)
{
wait_accountRepo.ChangePassword(user.Email、user.Username、passwordHash);
}
user.PasswordHash=PasswordHash;
}
公共异步任务GetPasswordHashAsync(登录用户,CancellationToken CancellationToken)
{
返回wait Task.FromResult(user.PasswordHash);
}
公共异步任务HasPasswordAsync(登录用户,CancellationToken CancellationToken)
{
返回wait Task.FromResult(!string.IsNullOrEmpty(user.PasswordHash)和&!string.IsNullOrEmpty(user.Salt));
}
公共任务SetEmailAsync(登录用户、字符串电子邮件、CancellationToken CancellationToken)
{
抛出新的NotImplementedException();
}
公共任务GetEmailAsync(登录用户,CancellationToken CancellationToken)
{
返回Task.FromResult(user.Email);
}
公共异步任务GetEmailConfirmedAsync(登录用户,CancellationToken CancellationToken)
{
返回wait Task.FromResult(user.emailconfirm);
}
公共任务SetEmailConfirmedAsync(登录用户、bool确认、CancellationToken CancellationToken)
{
返回Task.FromResult(user.emailconfirm=已确认);
}
公共任务findbyemailsync(字符串normalizedEmail、CancellationToken CancellationToken)
{
抛出新的NotImplementedException();
}
services.AddIdentity<User, Role>()
     .AddDefaultTokenProviders()
     .AddUserManager<SandboxCoreDapper.Identity.Services.UserManager<User>>()
     .AddRoleManager<SandboxCoreDapper.Identity.Services.RoleManager<Role>>()
            ;
public class UserStore : IUserPasswordStore<Login>, IUserEmailStore<Login>, IUserPhoneNumberStore<Login>
    {
        private readonly IAccountRepo _accountRepo;

        public UserStore(IAccountRepo accountRepo)
        {
            _accountRepo = accountRepo;
        }

        public void Dispose()
        {
        }

        public async Task<string> GetUserIdAsync(Login user, CancellationToken cancellationToken)
        {
            return await Task.FromResult(user.Username);
        }

        public async Task<string> GetUserNameAsync(Login user, CancellationToken cancellationToken)
        {
            return await Task.FromResult(user.Username);
        }

        public Task SetUserNameAsync(Login user, string userName, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public Task<string> GetNormalizedUserNameAsync(Login user, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public async Task SetNormalizedUserNameAsync(Login user, string normalizedName, CancellationToken cancellationToken)
        {
            await Task.FromResult(user.Username = normalizedName.ToLower());
        }

        public async Task<IdentityResult> CreateAsync(Login user, CancellationToken cancellationToken)
        {
            await _accountRepo.CreateLogin(user);
            return IdentityResult.Success;
        }

        public async Task<IdentityResult> UpdateAsync(Login user, CancellationToken cancellationToken)
        {
            await _accountRepo.UpdateLogin(user.LoginId, user.Email, true);
            return IdentityResult.Success;
        }

        public Task<IdentityResult> DeleteAsync(Login user, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public async Task<Login> FindByIdAsync(string userId, CancellationToken cancellationToken)
        {
            return await _accountRepo.GetUser(userId);
        }

        public async Task<Login> FindByNameAsync(string normalizedUserName, CancellationToken cancellationToken)
        {
            return await _accountRepo.GetUser(normalizedUserName);
        }

        public async Task SetPasswordHashAsync(Login user, string passwordHash, CancellationToken cancellationToken)
        {
            if (user.LoginId != 0)
            {
                await _accountRepo.ChangePassword(user.Email, user.Username, passwordHash);
            }

            user.PasswordHash = passwordHash;
        }

        public async Task<string> GetPasswordHashAsync(Login user, CancellationToken cancellationToken)
        {
            return await Task.FromResult(user.PasswordHash);
        }

        public async Task<bool> HasPasswordAsync(Login user, CancellationToken cancellationToken)
        {
            return await Task.FromResult(!string.IsNullOrEmpty(user.PasswordHash) && !string.IsNullOrEmpty(user.Salt));
        }

        public Task SetEmailAsync(Login user, string email, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public Task<string> GetEmailAsync(Login user, CancellationToken cancellationToken)
        {
            return Task.FromResult(user.Email);
        }

        public async Task<bool> GetEmailConfirmedAsync(Login user, CancellationToken cancellationToken)
        {
            return await Task.FromResult(user.EmailConfirmed);
        }

        public Task SetEmailConfirmedAsync(Login user, bool confirmed, CancellationToken cancellationToken)
        {
            return Task.FromResult(user.EmailConfirmed = confirmed);
        }

        public Task<Login> FindByEmailAsync(string normalizedEmail, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public Task<string> GetNormalizedEmailAsync(Login user, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public Task SetNormalizedEmailAsync(Login user, string normalizedEmail, CancellationToken cancellationToken)
        {
            return Task.FromResult(user.Email = normalizedEmail.ToLower());
        }

        public Task SetPhoneNumberAsync(Login user, string phoneNumber, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public Task<string> GetPhoneNumberAsync(Login user, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public Task<bool> GetPhoneNumberConfirmedAsync(Login user, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public Task SetPhoneNumberConfirmedAsync(Login user, bool confirmed, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }
    }
services.AddSingleton<IUserStore<Login>, UserStore>();