'Extending or override GetRolesAsync method in Asp.net Identity
Im using Asp.net Identity V2 on MVC 5 project.
Due to the nature of project , im trying to get List Of Custome Model
instead of String List from GetRolesAsync per userId. Is there any way
that i can Override this method to return my custome class , like the
one i is mapped to the table?
Also im using my own database layer and removed the EntityFrameWork part from Identity.
so there is no dbcontext.
this is my ApplicationUser :
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;
}
}
UserStore 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;
}
Solution 1:[1]
Can't you just add a custom interface between UserStore and IUserStore ? I wrote a small example.
UserStore extended Interface:
public interface ICustomUserStore<TUser>: IUserStore<TUser>, Interfaces.IUserRoleStore<TUser> where TUser : class
{
MyCustomClass GetCustomRolesAsync(ApplicationUser user);
}
New User Store 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;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Geoffrey Fook |