扩展或重写 Asp.net Identity 中的 GetRolesAsync 方法

Extending or override GetRolesAsync method in Asp.net Identity

我在 MVC 5 项目上使用 Asp.net Identity V2。

由于项目的性质,我正在尝试获取客户模型列表

而不是每个用户 ID 来自 GetRolesAsync 的字符串列表。有什么办法

我可以将此方法覆盖到 return 我的客户 class ,例如

一个 i 映射到 table?

我也使用我自己的数据库层并从 Identity 中删除了 EntityFrameWork 部分。

所以没有dbcontext。

这是我的应用程序用户:

public class ApplicationUser : UserInfo
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(IdentityStore.UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
}

用户存储 Class :

  public class UserStore : Interfaces.IUserStore<ApplicationUser>, Interfaces.IUserRoleStore<ApplicationUser>
    {
        #region IUserStore
        public Task CreateAsync(ApplicationUser user)
        {
            if (user != null)
            {
                return Task.Factory.StartNew(() =>
                {
                    UserDAL.NewUser(user);
                });
            }
            throw new ArgumentNullException("user");
        }

        public Task DeleteAsync(ApplicationUser user)
        {
            if (user != null)
            {
                return Task.Factory.StartNew(() =>
                {
                    UserDAL.DeleteUser(user);
                });
            }
            throw new ArgumentNullException("user");
        }

        public void Dispose()
        {

        }

        public Task<ApplicationUser> FindByIdAsync(int userId)
        {
            if (userId > 0)
            {
                return Task.Factory.StartNew(() =>
                {
                    return UserDAL.GetUser(userId);
                });
            }
            throw new ArgumentNullException("userId");
        }

        public Task<ApplicationUser> FindByNameAsync(string userName)
        {
            if (!string.IsNullOrEmpty(userName))
            {
                return Task.Factory.StartNew(() =>
                {
                    return UserDAL.GetUserByUsername(userName);
                });
            }
            throw new ArgumentNullException("userName");
        }

        public ApplicationUser FindByName(string userName)
        {
            if (!string.IsNullOrEmpty(userName))
            {
                return UserDAL.GetUserByUsername(userName);
            }
            throw new ArgumentNullException("userName");
        }

        public Task UpdateAsync(ApplicationUser user)
        {
            if (user != null)
            {
                return Task.Factory.StartNew(() =>
                {
                    return UserDAL.UpdateUser(user);
                });
            }
            throw new ArgumentNullException("userName");
        }
        #endregion

        #region IUserRoleStore
        public Task AddToRoleAsync(ApplicationUser user, string roleName)
        {
            if (user != null)
            {
                return Task.Factory.StartNew(() =>
                {
                    UserRoleDAL.NewUserRole(user.Id, roleName);
                });
            }
            else
            {
                throw new ArgumentNullException("user");
            }
        }

        public Task RemoveFromRoleAsync(ApplicationUser user, string roleName)
        {
            if (user != null)
            {
                return Task.Factory.StartNew(() =>
                {
                    UserRoleDAL.DeleteUserRole(user.Id, roleName);
                });
            }
            else
            {
                throw new ArgumentNullException("user");
            }
        }

        public Task<IList<string>> GetRolesAsync(ApplicationUser user)
        {
            if (user != null)
            {
                return Task.Factory.StartNew(() =>
                {
                    IList<string> roles = UserRoleDAL.GetUserRoles(user.Id);
                    return roles;
                });
            }
            else
            {
                throw new ArgumentNullException("user");
            }
        }

        public Task<bool> IsInRoleAsync(ApplicationUser user, string roleName)
        {
            if (user != null)
            {
                return Task.Factory.StartNew(() =>
                {
                    IList<RoleInfo> roles = UserRoleDAL.GetUserRoles(user.Id);
                    foreach (RoleInfo role in roles)
                    {
                        if (role.Name.ToUpper() == roleName.ToUpper())
                        {
                            return true;
                        }
                    }

                    return false;
                });
            }
            else
            {
                throw new ArgumentNullException("user");
            }
        }
        public IQueryable<UserInfo> Users => (UserDAL.GetUsers()).AsQueryable();

        #endregion
    }

UserRoleDAL Class:

public static int NewUserRole(int userID, string roleName)
    {
        List<ParameterInfo> parameters = new List<ParameterInfo>();
        parameters.Add(new ParameterInfo() { ParameterName = "UserID", ParameterValue = userID });
        parameters.Add(new ParameterInfo() { ParameterName = "RoleName", ParameterValue = roleName });
        int success = SqlHelper.ExecuteQuery("NewUserRole", parameters);
        return success;
    }

    public static int DeleteUserRole(int userID, string roleName)
    {
        List<ParameterInfo> parameters = new List<ParameterInfo>();
        parameters.Add(new ParameterInfo() { ParameterName = "UserID", ParameterValue = userID });
        parameters.Add(new ParameterInfo() { ParameterName = "RoleName", ParameterValue = roleName });
        int success = SqlHelper.ExecuteQuery("DeleteUserRole", parameters);
        return success;
    }

    public static IList<string> GetUserRoles(int userID)
    {
        List<ParameterInfo> parameters = new List<ParameterInfo>();
        parameters.Add(new ParameterInfo() { ParameterName = "UserID", ParameterValue = userID });
        IList<string> roles = SqlHelper.GetRecords<string>("GetUserRoles", parameters);
        return roles;
    }

    public static UserInfo GetUserByUsername(string userName)
    {
        List<ParameterInfo> parameters = new List<ParameterInfo>();
        parameters.Add(new ParameterInfo() { ParameterName = "Username", ParameterValue = userName });
        UserInfo oUser = SqlHelper.GetRecord<UserInfo>("GetUserByUsername", parameters);
        return oUser;
    }

    public static List<RoleInfo> GetAllRole()
    {
        List<RoleInfo> oUser = SqlHelper.GetRecord<List<RoleInfo>>("GetAllRole",null);
        return oUser;
    }

你不能在 UserStoreIUserStore 之间添加自定义接口吗? 我写了一个小例子。

UserStore 扩展接口:

public interface ICustomUserStore<TUser>: IUserStore<TUser>, Interfaces.IUserRoleStore<TUser> where TUser : class
{
    MyCustomClass GetCustomRolesAsync(ApplicationUser user);
}

新用户商店Class:

public class UserStore : ICustomUserStore<ApplicationUser>
{
 
    public MyCustomClass GetCustomRolesAsync(ApplicationUser user)
    {
        if (user != null)
        {
            return Task.Factory.StartNew(() =>
            {
                var customRoles = UserRoleDAL.GetCustomUserRoles(user.Id);
                return customRoles;
            });
        }
        else
        {
            throw new ArgumentNullException("user");
        }
    }

    //Implement missing methods

}

UserRoleDAL Class:

public static MyCustomClass GetCustomUserRoles(int userID)
{
    var roles = new MyCustomClass();
    //Do stuff to populate your customRolesClass
   
    return roles;
}