我有自己的自定义实体商店用户和实体商店角色。我现在正在尝试的是将用户添加到角色。通过这样做
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>
}
}
之后我实现了。我现在有这个功能
public Task<IList<string>> GetRolesAsync(TUser user)
{
throw new NotImplementedException();
}
我该如何实施?我的意思是如何发送用户拥有的角色?我的完整实体用户存储如下
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();
}
}
我现在正在尝试这个
public Task<IList<string>> GetRolesAsync(TUser user)
{
List<string> roles = new UserManager<TUser>(this).GetRoles(user.Id).ToList();
return Task.FromResult((IList<string>)roles);
}
但它不起作用。我得到了例外的时间。
答案 0 :(得分:2)
您只需按如下方式实现以下接口IUserRoleStore<TUser>
:
public class EntityUserStore<TUser, TAccount> : IUserStore<TUser>,
IUserPasswordStore<TUser>,
IUserEmailStore<TUser> , IUserRoleStore<TUser>, IUserRoleStore<TUser>
然后强制您实现以下方法的具体实现;
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();
}
您只需针对您的数据源提供每种方法的实现。
第一个的简化示例如下(取决于您的数据上下文等):
public Task AddToRoleAsync(TUser user, string roleName)
{
var roleToAdd = _context.Roles.FirstOrDefault(r => r.Name == roleName);
user.Roles.Add(roleToAdd);
return _context.SaveChangesAsync();
}
<强>更新强>
以下是来自Stuart Leeks leeksnet.AspNet.Identity.TableStorage 的已发布示例,这是您尝试实现的示例。
看一下这堂课:UserStore.cs