Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.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
C# ASP.NET Identity PasswordHasher在每个请求上生成不同的哈希_C#_Asp.net_Entity Framework_Asp.net Mvc 5_Asp.net Identity - Fatal编程技术网

C# ASP.NET Identity PasswordHasher在每个请求上生成不同的哈希

C# ASP.NET Identity PasswordHasher在每个请求上生成不同的哈希,c#,asp.net,entity-framework,asp.net-mvc-5,asp.net-identity,C#,Asp.net,Entity Framework,Asp.net Mvc 5,Asp.net Identity,我遇到了一个特殊的问题,我正在研究一个解决方案。它使用ASP.NET标识系统和我为处理现有数据而构建的自定义实现。直到昨天,它还运行得很好,但不知怎么的,我再也无法登录了。据我所知,密码散列器似乎在每个请求上生成不同的散列,其中没有一个与存储在数据库中的散列匹配 是不是我做错了什么让这件事发生了?不确定这是否重要,但我正在制作从办公室电脑下载的解决方案的本地副本,因为RDP变得很烦人。它曾经在本地工作过,但我认为当我将数据库种子设定从基于对象改为直接的SQL脚本时,它停止了。考虑到我插入了相同的

我遇到了一个特殊的问题,我正在研究一个解决方案。它使用ASP.NET标识系统和我为处理现有数据而构建的自定义实现。直到昨天,它还运行得很好,但不知怎么的,我再也无法登录了。据我所知,密码散列器似乎在每个请求上生成不同的散列,其中没有一个与存储在数据库中的散列匹配

是不是我做错了什么让这件事发生了?不确定这是否重要,但我正在制作从办公室电脑下载的解决方案的本地副本,因为RDP变得很烦人。它曾经在本地工作过,但我认为当我将数据库种子设定从基于对象改为直接的SQL脚本时,它停止了。考虑到我插入了相同的值,我不确定为什么这会使它不起作用,但在这里,我无法进行身份验证

更新

我使用的
UserManager
通过Ninject传递到我的
UserStore
实例中。这是我的
UserStore
的代码:

public class EmployeeStore : IQueryableUserStore<Employee, int>, IUserStore<Employee, int>, IUserPasswordStore<Employee, int>, IUserRoleStore<Employee, int>, IDisposable {
    private bool Disposed;
    private IDatabaseRepository<Role> RolesRepository { get; set; }
    private IDatabaseRepository<Employee> EmployeesRepository { get; set; }

    public EmployeeStore(
        IDatabaseRepository<Role> rolesRepository,
        IDatabaseRepository<Employee> employeesRepository) {
        this.RolesRepository = rolesRepository;
        this.EmployeesRepository = employeesRepository;
    }

    #region IQueryableUserStore Members
    public IQueryable<Employee> Users {
        get {
            return this.EmployeesRepository.Set;
        }
    }
    #endregion

    #region IUserStore Members
    public async Task CreateAsync(
        Employee employee) {
        this.ThrowIfDisposed();

        if (employee == null) {
            throw new ArgumentNullException("employee");
        }

        await this.EmployeesRepository.AddAndCommitAsync(employee);
    }

    public async Task DeleteAsync(
        Employee employee) {
        this.ThrowIfDisposed();

        if (employee == null) {
            throw new ArgumentNullException("employee");
        }

        await this.EmployeesRepository.RemoveAndCommitAsync(employee);
    }

    public Task<Employee> FindByIdAsync(
        int employeeId) {
        this.ThrowIfDisposed();

        return Task.FromResult<Employee>(this.EmployeesRepository.FindSingleOrDefault(
            u =>
                (u.Id == employeeId)));
    }

    public Task<Employee> FindByNameAsync(
        string userName) {
        this.ThrowIfDisposed();

        return Task.FromResult<Employee>(this.EmployeesRepository.FindSingleOrDefault(
            e =>
                (e.UserName == userName)));
    }

    public async Task UpdateAsync(
        Employee employee) {
        this.ThrowIfDisposed();

        if (employee == null) {
            throw new ArgumentNullException("employee");
        }

        await this.EmployeesRepository.CommitAsync();
    }
    #endregion

    #region IDisposable Members
    public void Dispose() {
        this.Dispose(true);

        GC.SuppressFinalize(this);
    }

    protected void Dispose(
        bool disposing) {
        this.Disposed = true;
    }

    private void ThrowIfDisposed() {
        if (this.Disposed) {
            throw new ObjectDisposedException(base.GetType().Name);
        }
    }
    #endregion

    #region IUserPasswordStore Members
    public Task<string> GetPasswordHashAsync(
        Employee employee) {
        this.ThrowIfDisposed();

        if (employee == null) {
            throw new ArgumentNullException("employee");
        }

        return Task.FromResult<string>(employee.PasswordHash);
    }

    public Task<bool> HasPasswordAsync(
        Employee employee) {
        return Task.FromResult<bool>(!String.IsNullOrEmpty(employee.PasswordHash));
    }

    public Task SetPasswordHashAsync(
        Employee employee,
        string passwordHash) {
        this.ThrowIfDisposed();

        if (employee == null) {
            throw new ArgumentNullException("employee");
        }

        employee.PasswordHash = passwordHash;

        return Task.FromResult<int>(0);
    }
    #endregion

    #region IUserRoleStore Members
    public Task AddToRoleAsync(
        Employee employee,
        string roleName) {
        this.ThrowIfDisposed();

        if (employee == null) {
            throw new ArgumentNullException("employee");
        }

        if (String.IsNullOrEmpty(roleName)) {
            throw new ArgumentNullException("roleName");
        }

        Role role = this.RolesRepository.FindSingleOrDefault(
            r =>
                (r.Name == roleName));

        if (role == null) {
            throw new InvalidOperationException("Role not found");
        }

        employee.Roles.Add(role);

        return Task.FromResult<int>(0);
    }

    public Task<IList<string>> GetRolesAsync(
        Employee employee) {
        this.ThrowIfDisposed();

        if (employee == null) {
            throw new ArgumentNullException("employee");
        }

        return Task.FromResult<IList<string>>(employee.Roles.Select(
            r =>
                r.Name).ToList());
    }

    public Task<bool> IsInRoleAsync(
        Employee employee,
        string roleName) {
        this.ThrowIfDisposed();

        if (employee == null) {
            throw new ArgumentNullException("employee");
        }

        if (String.IsNullOrEmpty(roleName)) {
            throw new ArgumentNullException("roleName");
        }

        return Task.FromResult<bool>(employee.Roles.Any(
            r =>
                (r.Name == roleName)));
    }

    public Task RemoveFromRoleAsync(
        Employee employee,
        string roleName) {
        this.ThrowIfDisposed();

        if (employee == null) {
            throw new ArgumentNullException("employee");
        }

        if (String.IsNullOrEmpty(roleName)) {
            throw new ArgumentNullException("roleName");
        }

        Role role = this.RolesRepository.FindSingleOrDefault(
            r =>
                (r.Name == roleName));

        if (role == null) {
            throw new InvalidOperationException("Role is null");
        }

        employee.Roles.Remove(role);

        return Task.FromResult<int>(0);
    }
    #endregion
}
公共类EmployeeStore:IQueryableUserStore、IUserStore、IUserPasswordStore、IUserRoleStore、IDisposable{
私人住宅;
私有IDatabaseRepository角色存储库{get;set;}
私有IDatabaseRepository EmployeesRepository{get;set;}
公共雇员商店(
IDatabaseRepository角色存储库,
IDatabaseRepository(员工储备){
this.RolesRepository=RolesRepository;
this.EmployeesRepository=EmployeesRepository;
}
#区域IQueryableUserStore成员
公共IQueryable用户{
得到{
返回此.EmployeesRepository.Set;
}
}
#端区
#区域IUserStore成员
公共异步任务CreateAsync(
(雇员){
这个.ThrowIfDisposed();
if(employee==null){
抛出新的异常(“员工”);
}
等待此消息。EmployeesRepository.addandcommittesync(雇员);
}
公共异步任务DeleteAsync(
(雇员){
这个.ThrowIfDisposed();
if(employee==null){
抛出新的异常(“员工”);
}
等待此消息。EmployeesRepository.RemoveAndCommitAsync(employee);
}
公共任务FindByIdAsync(
国际雇员ID){
这个.ThrowIfDisposed();
返回Task.FromResult(this.EmployeesRepository.FindSingleOrDefault(
u=>
(u.Id==雇员Id));
}
公共任务FindByNameAsync(
字符串(用户名){
这个.ThrowIfDisposed();
返回Task.FromResult(this.EmployeesRepository.FindSingleOrDefault(
e=>
(e.UserName==UserName));
}
公共异步任务UpdateAsync(
(雇员){
这个.ThrowIfDisposed();
if(employee==null){
抛出新的异常(“员工”);
}
等待此消息。EmployeesRepository.CommitAsync();
}
#端区
#区域IDisposable成员
公共空间处置(){
这个。处置(真实);
总干事(本);
}
受保护的无效处置(
布尔(美国){
这是真的;
}
私有void ThrowIfDisposed(){
如果(本文件已处理){
抛出新的ObjectDisposedException(base.GetType().Name);
}
}
#端区
#区域IUserPasswordStore成员
公共任务GetPasswordHashAsync(
(雇员){
这个.ThrowIfDisposed();
if(employee==null){
抛出新的异常(“员工”);
}
返回Task.FromResult(employee.PasswordHash);
}
公共任务HasPasswordAsync(
(雇员){
返回Task.FromResult(!String.IsNullOrEmpty(employee.PasswordHash));
}
公共任务SetPasswordHashAsync(
雇员,,
字符串密码(哈希){
这个.ThrowIfDisposed();
if(employee==null){
抛出新的异常(“员工”);
}
employee.PasswordHash=PasswordHash;
返回Task.FromResult(0);
}
#端区
#区域IUserRoleStore成员
公共任务AddToRoleAsync(
雇员,,
字符串(roleName){
这个.ThrowIfDisposed();
if(employee==null){
抛出新的异常(“员工”);
}
if(String.IsNullOrEmpty(roleName)){
抛出新的ArgumentNullException(“roleName”);
}
Role Role=this.RolesRepository.FindSingleOrDefault(
r=>
(r.Name==roleName));
如果(角色==null){
抛出新的InvalidOperationException(“未找到角色”);
}
employee.Roles.Add(角色);
返回Task.FromResult(0);
}
公共任务GetRolesAsync(
(雇员){
这个.ThrowIfDisposed();
if(employee==null){
抛出新的异常(“员工”);
}
返回Task.FromResult(employee.Roles.Select(
r=>
r、 名称);
}
公共任务是异步的(
雇员,,
字符串(roleName){
这个.ThrowIfDisposed();
if(employee==null){
抛出新的异常(“员工”);
}
if(String.IsNullOrEmpty(roleName)){
抛出新的ArgumentNullException(“roleName”);
}
返回Task.FromResult(employee.Roles.Any(
r=>
(r.Name==roleName));
}
从RoleAsync删除的公共任务(
雇员,,
字符串(roleName){
这个.ThrowIfDisposed();
if(employee==null){
抛出新的异常(“员工”);
}
if(String.IsNullOrEmpty(roleName)){
抛出新的Argu
public sealed class SiteController : BaseController {
    private IAuthenticationManager AuthenticationManager {
        get {
            return this.HttpContext.GetOwinContext().Authentication;
        }
    }

    private UserManager<Employee, int> EmployeeManager { get; set; }

    public SiteController(
        UserManager<Employee, int> employeeManager) {
        this.EmployeeManager = employeeManager;
    }

    [HttpGet, AllowAnonymous]
    public ActionResult Default() {
        return base.View();
    }

    [HttpPost, AllowAnonymous, ValidateAntiForgeryToken]
    public async Task<RedirectToRouteResult> Default(
        [Bind(Prefix = "Credentials", Include = "Email,Password")] Credentials credentials) {
        if (this.ModelState.IsValid) {
            Employee employee = await EmployeeManager.FindAsync(credentials.Email, credentials.Password);

            if (employee != null) {
                ClaimsIdentity identityClaim = await this.EmployeeManager.CreateIdentityAsync(employee, DefaultAuthenticationTypes.ApplicationCookie);

                if (identityClaim != null) {
                    AuthenticationManager.SignIn(new AuthenticationProperties(), identityClaim);

                    return base.RedirectToAction("Dashboard");
                }
            }
        }

        return base.RedirectToAction("Default");
    }

    [HttpGet]
    public RedirectToRouteResult SignOut() {
        this.AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);

        return base.RedirectToAction("Default");
    }
}