mohsinali1317
mohsinali1317

Reputation: 4425

Store does not implement IUserRoleStore<TUser>

I have my own customized Entity Store User and Entity Store Role. What I am trying now is to add user to roles. By doing this

public void CreateUser(RegisterBindingModel model, String roleName)
   {
        try
        {
            ApplicationRole role = null;
            if (!UserRoleManager.RoleExists(roleName))
            {
                role = new ApplicationRole() { Name = roleName, Id = Suid.NewSuid().ToString() };
                IdentityResult result = UserRoleManager.Create(role);
            }
            else
            {
                role = UserRoleManager.FindByName(roleName);
            }

            var user = new ApplicationUser() { UserName = model.Email, Email = model.Email };

            IdentityResult result1 = UserManager.Create(user, model.Password);

            UserManager.AddToRole(user.Id, role.Id);

        }
        catch (Exception e)
        {
            throw new Exception(e.Message.ToString()); // I get an exception here saying, Store does not implement IUserRoleStore<TUser>
        }

  }

After that I implement that. I have this function now

    public Task<IList<string>> GetRolesAsync(TUser user)
    {

        throw new NotImplementedException();
    }

How do I implement it? I mean how to I send in the roles the user have? My complete Entity user store is as follows

    public class EntityUserStore<TUser, TAccount> : IUserStore<TUser>, IUserPasswordStore<TUser>, IUserEmailStore<TUser> ,  IUserRoleStore<TUser>
    where TUser : class, IApplicationUser<TAccount>, new()
    where TAccount : EntityModel, new()
{
    public async Task CreateAsync(TUser user)
    {
        TableHelper.Save<TAccount>(user.Account);
        EntityIndex.Set<TAccount>(user.UserName, user.Account);
    }

    public Task DeleteAsync(TUser user)
    {
        throw new NotImplementedException();
    }

    public void Dispose()
    {

    }

    public async Task<TUser> FindByIdAsync(string userId)
    {
        TAccount account = TableHelper.Get<TAccount>(userId);

        if (account == null)
            return null;

        return new TUser()
        {
            Account = account
        };

    }

    public async Task<TUser> FindByNameAsync(string userName)
    {
        TAccount account = EntityIndex.Get<TAccount>(userName);

        if (account == null)
            return null;

        return new TUser()
        {
            Account = account
        };
    }

    public Task UpdateAsync(TUser user)
    {
        throw new NotImplementedException();
    }

    public async Task SetPasswordHashAsync(TUser user, string passwordHash)
    {
        user.PasswordHash = passwordHash;

    }

    public async Task<string> GetPasswordHashAsync(TUser user)
    {
        return user.PasswordHash;
    }

    public async Task<bool> HasPasswordAsync(TUser user)
    {
        return user.PasswordHash != null;
    }

    public async Task SetEmailAsync(TUser user, string email)
    {
        user.Email = email;
    }

    public async Task<string> GetEmailAsync(TUser user)
    {
        return user.Email;
    }

    public Task<bool> GetEmailConfirmedAsync(TUser user)
    {
        throw new NotImplementedException();
    }

    public Task SetEmailConfirmedAsync(TUser user, bool confirmed)
    {
        throw new NotImplementedException();
    }

    public async Task<TUser> FindByEmailAsync(string email)
    {
        TAccount account = EntityIndex.Get<TAccount>(email);

        if (account == null)
            return null;

        return new TUser()
        {
            Account = account
        };
    }

    public Task AddToRoleAsync(TUser user, string roleName)
    {
        throw new NotImplementedException();
    }

    public Task RemoveFromRoleAsync(TUser user, string roleName)
    {
        throw new NotImplementedException();
    }

    public Task<IList<string>> GetRolesAsync(TUser user)
    {

        throw new NotImplementedException();
    }

    public Task<bool> IsInRoleAsync(TUser user, string roleName)
    {
        throw new NotImplementedException();
    } 
}

I am now trying this

    public Task<IList<string>> GetRolesAsync(TUser user)
    {
        List<string> roles = new UserManager<TUser>(this).GetRoles(user.Id).ToList();
        return Task.FromResult((IList<string>)roles);
    }

But it is not working. I am getting timed out exception.

Upvotes: 0

Views: 7468

Answers (1)

hutchonoid
hutchonoid

Reputation: 33316

You would simply implement the following interface IUserRoleStore<TUser> as follows:

public class EntityUserStore<TUser, TAccount> : IUserStore<TUser>, 
IUserPasswordStore<TUser>, 
IUserEmailStore<TUser> ,  IUserRoleStore<TUser>, IUserRoleStore<TUser>

This then enforces that you implement concrete implementations of the following methods;

    public Task AddToRoleAsync(TUser user, string roleName)
    {
        throw new NotImplementedException();
    }

    public Task RemoveFromRoleAsync(TUser user, string roleName)
    {
        throw new NotImplementedException();
    }

    public Task<IList<string>> GetRolesAsync(TUser user)
    {
        throw new NotImplementedException();
    }

    public Task<bool> IsInRoleAsync(TUser user, string roleName)
    {
        throw new NotImplementedException();
    }

You would simply provide the implementation of each of the methods against your datasource.

A simplified example of the first one could be as follows (depending on your data context etc):

    public Task AddToRoleAsync(TUser user, string roleName)
    {
        var roleToAdd = _context.Roles.FirstOrDefault(r => r.Name == roleName);
        user.Roles.Add(roleToAdd);
        return _context.SaveChangesAsync();
    }

Update

Here is a published example from Stuart Leeks leeksnet.AspNet.Identity.TableStorage, this has an example of what you are trying to achieve.

Take a look at this class: UserStore.cs

Upvotes: 2

Related Questions