【发布时间】:2020-03-27 09:19:45
【问题描述】:
我们有具有角色成员资格的 MVC 应用程序和用于 SSO 的 IdentityServer,两者都是独立的应用程序。我们的应用程序中有两个角色——Admin 和 RegularUser。它们每个都有一组特定的属性,例如:
public class Admin
{
public string Area { get; set; }
public int Code { get; set;}
}
public class RegularUser
{
public string SkypeId { get; set; }
public string HomeAddress { get; set; }
}
根据IdentityServer template,我们应该使用单个 ApplicationUser 类来存储所有配置文件数据。跨多个应用程序存储特定于组织角色的数据的最佳实践是什么?我的想法是将配置文件存储(ApplicationUser,在 IdentityServer 范围内)与域实体(Admin 和 RegularUser,在 MVC 应用程序范围内)分开,并用一个键将它们链接在一起:
public class Admin
{
public string ApplicationUserId { get; set; }
public string Area { get; set; }
public int Code { get; set;}
}
public class RegularUser
{
public string ApplicationUserId { get; set; }
public string SkypeId { get; set; }
public string HomeAddress { get; set; }
}
当我们在成功登录到 MVC 应用程序后从 SSO 重定向时,我们可以像这样在控制器中获取 Admin 或 RegularUser:
[Authorize]
public async Task<IActionResult> SomeAction()
{
string userId = _userManager.GetUserId(User);
if (User.IsInRole("Admin"))
{
Admin admin = await _adminRepository.GetByApplicationUserId(userId);
/* logic for admin */
}
if (User.IsInRole("RegularUser"))
{
RegularUser regularUser = await _regularUserRepository.GetByApplicationUserId(userId);
/* logic for regularUser */
}
...
}
【问题讨论】:
标签: identityserver4