我有我自己的自定义实体店用户和实体店角色。我现在正在尝试将用户添加到角色。通过这样做商店不实施IUserRoleStore <TUser>
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);
}
但它无法正常工作。我正在超时例外。
请问,有没有你需要创建自己的实现的一个原因?我问,因为当我第一次使用ASP.NET Identity时,我开始做同样的事情。然后我意识到我可以使用框架中已经存在的实现,它的工作方式很轻松。 – Luke
它是由我的老板完成的,但我认为原因是存储机制。我们不使用Sql,而是使用Azure。 – mohsinali1317
无论您是否使用Azure,它仍然需要数据存储。如果它不是SQL,它是什么? – Luke