简体   繁体   中英

Custom role storage asp.net

I have followed this link and tried to make my own Roles thing because I am using Azure as storage instead of Sql.

public interface IApplicationUserRole : IRole<string>
{

}

public class EntityUserRoleStore<TRole> : IRoleStore<TRole,string>, IRoleStore<TRole>
     where TRole : class, IApplicationUserRole, new()
{
    static List<TRole> roles = new List<TRole>();

    public async Task CreateAsync(TRole role)
    {
        if (role == null)
        {
            throw new ArgumentNullException("role");
        }

        roles.Add(role);

        // return Task.FromResult<object>(null);

    }

    public async Task DeleteAsync(TRole role)
    {
        if (role == null)
        {
            throw new ArgumentNullException("role");
        }

        roles.Remove(role);

    }

    public void Dispose()
    {
    }

    public async Task<TRole> FindByIdAsync(string roleId) 
    {
        if (roleId == null)
        {
            throw new ArgumentNullException("role");
        }

        return roles.Where(i => i.Id == roleId).FirstOrDefault();

    }


    public async Task<TRole> FindByNameAsync(string roleName)
    {
        if (roleName == null)
        {
            throw new ArgumentNullException("role");
        }

        return roles.Where(i => i.Name == roleName).FirstOrDefault();
    }

    public async Task UpdateAsync(TRole role)
    {
        if (role == null)
        {
            throw new ArgumentNullException("role");
        }

        roles.Where(i => i.Id == role.Id).FirstOrDefault().Name = role.Name;
    }
}

Then Application role model looks like this

public class ApplicationRole : IdentityRole, IApplicationUserRole  
{
}

after that the application manager class looks like this

public class ApplicationRoleManager : RoleManager<ApplicationRole>
{
    public ApplicationRoleManager(IRoleStore<ApplicationRole> store)
        : base(store)
    {
    }

    public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
    {
        var manager = new ApplicationRoleManager(new EntityUserRoleStore<ApplicationRole>());
        return manager;
    }
}

The final bit is when I create the Manager in

Startup class, like this

 public void ConfigureAuth(IAppBuilder app)
    {
        // Configure the db context and user manager to use a single instance per request
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

        app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
   }

After all this configuration I am trying to create a Role like this in my Test controller

 public void CreateRole(string roleName)
    {
        var role = new ApplicationRole() { Name = roleName, Id = Suid.NewSuid().ToString() };
        IdentityResult result = UserRoleManager.Create(role);
    }

The UserRoleManager is defined like this

    public ApplicationRoleManager UserRoleManager
    {
        get
        {
            return _userRoleManager ?? Request.GetOwinContext().Get<ApplicationRoleManager>();
        }
        private set
        {
            _userRoleManager = value;
        }
    }

After creating a role I am not able to see it. I try doing

UserRoleManager.Roles.Count();

but it gives me this error

"Store does not implement IQueryableRoleStore."

Anyone knows what am I missing here?

Your EntityUserRoleStore needs to implement the IQueryableRoleStore interface.

Replace the following:

public class EntityUserRoleStore<TRole> : IRoleStore<TRole,string>, IRoleStore<TRole>

With this:

public class EntityUserRoleStore<TRole> : IQueryableRoleStore<TRole>

Please note this will then require you to implement the interfaces, including the public IQueryable<TRole> Roles which appears to be missing.

If your primary key is a string the above will work, if it is something different you will need to implement the relevant interface. Below is an example of a guid.

public class OracleRoleStore<TRole> : IQueryableRoleStore<TRole, Guid>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM