我们有具有角色成员身份的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类来存储所有配置文件数据。在多个应用程序中组织特定于组织角色的数据的最佳实践是什么?我的想法是将配置文件存储(在IdentityServer范围内为ApplicationUser)与域实体(在MVC应用程序范围内为Admin和RegularUser)分开,并用键将它们链接在一起:
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 */
}
...
}
您也可以使用IdentityServer进行授权。
为每个应用程序定义一个IdentityResource,并为其定义一些UserClaimType。
new IdentityResource("app1_permissions", new string[] { "permission_in_app1" });
使用用户在app1中的权限填充permission_in_app1声明。
new Claim("permission_in_app1","[read1,write2]")
在您的客户端中,当您重定向用户时,请从IdentityServer请求app1_permissions范围。
scope: app1_permissions openid profile