我在MVC 5项目中使用Asp.net Identity V2。
由于项目的性质,我试图获得自定义模型列表
而不是从GetRolesAsync每个userId的字符串列表。有没有办法
,我可以重写这个方法来返回我的自定义类,如
一个I映射到表?
我还使用了我自己的数据库层,并从Identity中删除了EntityFrameWork部分。
所以没有dbcontext。
这是我的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类:
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类:
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;
}
你就不能在UserStore之间添加一个自定义接口吗?和IUserStore?我写了一个小例子。
UserStore扩展接口:
public interface ICustomUserStore<TUser>: IUserStore<TUser>, Interfaces.IUserRoleStore<TUser> where TUser : class
{
MyCustomClass GetCustomRolesAsync(ApplicationUser user);
}
新用户存储类:
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类:
public static MyCustomClass GetCustomUserRoles(int userID)
{
var roles = new MyCustomClass();
//Do stuff to populate your customRolesClass
return roles;
}