Identity UserManager会话错误

时间:2018-06-13 22:19:29

标签: asp.net-mvc asp.net-identity

我尝试为我的项目添加标识。

您可以查看git project,然后转到" 帐户/注册"看到我的问题。

请注意,在代码中我有两个UserManager。一个用于处理数据库会话(在IdentityStore内部 - UserManager userManager),第二个用于标识(UserManager<User, string>)。

我使用Nhibernate来处理数据库,所以这里是我的会话结构:

会话提供商

public class SessionProvider:IDisposable
{
    private ITransaction transaction;

    #region HttpContext
    private ISession GetHttpContextSession =>
        (ISession)HttpContext.Current.Items["session"];

    private bool IsNullHttpContextSession =>
        GetHttpContextSession == null;

    private bool IsOpenHttpContextSession =>
        !IsNullHttpContextSession ? GetHttpContextSession.IsOpen : false;

    private ITransaction GetHttpContextTransaction =>
        Session.Transaction;

    #endregion

    public ISession Session
    {
        get
        {
            var current = HttpContext.Current;

            if (IsNullHttpContextSession || !IsOpenHttpContextSession)
            {
                if (!current.Items.Contains("session"))
                    current.Items.Add("session", SessionHelper.OpenSession());
                else
                    current.Items["session"] = SessionHelper.OpenSession();
            }

            return GetHttpContextSession;
        }
    }

    public Task BeginTransaction()
    {
        if (transaction == null)
        {
            if (!GetHttpContextTransaction.IsActive)
                Session.BeginTransaction();

            transaction = GetHttpContextTransaction;
        }

        return Task.CompletedTask;
    }

    public async Task Commit()
    {
        try
        {
            if (transaction != null && transaction.IsActive)
                await transaction.CommitAsync();
        }
        catch
        {
            if (transaction != null && transaction.IsActive)
                await transaction.RollbackAsync();
        }
        finally
        {
            Dispose();
        }
    }

    public void Dispose()
    {
        Session.Dispose();
    }
}

我创建了从UserStore继承的Identity Manager(SignIn和UserManager)和IdentityStore。

Identity structure

比我试图硬编码用户注册:

public async Task<ActionResult> Register(RegisterViewModel model)
{
    var identS = new IdentityStore();
    var userManager = new IdentityUserManager(identS);

    var user = new User() { UserName = model.Login, Email = model.Email, PasswordHash = model.Password, Role = "User" };

    await userManager.CreateAsync(user);

    return RedirectToAction("Index", "Home");
}

但它有一个错误: Error screen

我可以使用IdentityStore轻松注册用户,它会起作用。但我需要IdentityUserManager。如何修复会话的工作?

身份代码:

 public class IdentityStore : IUserStore<User, string>, IUserLoginStore<User, string>
    {
        private UserProvider userProvider;
        private UserManager userManager;

        public IdentityStore()
        {
            this.userProvider = new UserProvider(new SessionProvider());
            this.userManager = new UserManager(userProvider);
        }

        #region IUserStore<User, string>

        public Task CreateAsync(User user)
        {
            return userManager.CreateUser(user);
        }

        public Task DeleteAsync(User user)
        {
            return  userManager.DeleteUser(user.Id);
        }

        public Task<User> FindByIdAsync(string userId)
        {
            return userManager.GetById(userId);
        }

        public Task UpdateAsync(User user)
        {
            return userManager.UpdateUser(user);
        }

        public Task<User> FindByNameAsync(string userName)
        {
            return userManager.GetByName(userName);
        }

        public Task<IList<UserLoginInfo>> GetLoginsAsync(User user)
        {
            return Task.Run(() =>
            {
                IList<UserLoginInfo> list = new List<UserLoginInfo>
                {
                    new UserLoginInfo("Local",user.Id)
                };

                return list;
            });
        }

        #endregion

        #region IUserLoginStore

        public Task RemoveLoginAsync(User user, UserLoginInfo login)
        {
            throw new NotImplementedException();
        }

        public Task<User> FindAsync(UserLoginInfo login)
        {
            return userManager.GetById(login.ProviderKey);
        }

        public Task AddLoginAsync(User user, UserLoginInfo login)
        {
            return Task.Run(() => user.Id = login.ProviderKey);
        }

        #endregion

        public void Dispose()
        {

        }
    }

和IdentityUserManager:

public class IdentityUserManager : UserManager<User, string>
{
    public IdentityUserManager(IUserStore<User, string> store)
        : base(store)
    {
        UserValidator = new UserValidator<User, string>(this);
        PasswordValidator = new PasswordValidator() { RequiredLength = 6 };
    }
}

0 个答案:

没有答案