ABP(ASP.NET Boilerplate)允许用户将自定义属性添加到用户实体中,然后将其添加到用户的Claims中。以下是将AppUser实体中的自定义属性添加到Claims中的示例代码:
public class AppUser : FullAuditedEntity
public static class AbpClaimTypes { // An example of using namespace and nested class to define claim type public static class MyCustomClaims { private const string Prefix = "mycustomclaims."; public const string MyCustomProperty = Prefix + "mycustomproperty"; } }
public static class UserClaimExtension { public static void AddMyCustomPropertyClaim(this ClaimsIdentity identity, string myCustomProperty) { identity.AddClaim(new Claim(AbpClaimTypes.MyCustomClaims.MyCustomProperty, myCustomProperty)); } }
// Then use it in your app: public class MyApplicationService : ApplicationService { private readonly UserManager _userManager;
public MyApplicationService(UserManager userManager) { _userManager = userManager; }
public async Task GetMyCustomProperty() { var user = await _userManager.GetUserAsync(AbpSession.GetUserId());
// Add custom claims
((ClaimsIdentity)AbpSession.Principal.Identity).AddMyCustomPropertyClaim(user.MyCustomProperty);
// Get custom claims
var myCustomPropertyClaim = AbpSession.Principal.FindFirst(AbpClaimTypes.MyCustomClaims.MyCustomProperty);
if (myCustomPropertyClaim != null)
{
string myCustomPropertyValue = myCustomPropertyClaim.Value;
//...
}
} }