您可以使用ExtendedProperty,将属性添加到AAD(Azure Active Directory)中的应用程序注册给用户,然后作为值......
ExtensionProperty identidadSecreta = new ExtensionProperty
{
Name = "LaIdentidadSecreta",
DataType = "String",
TargetObjects = { "User" }
};
myApplication.ExtensionProperties.Add(identidadSecreta);
await myApplication.UpdateAsync();
client.Context.SaveChanges();
然后对用户:
User user = null;
try
{
ActiveDirectoryClient client = AuthenticationHelper.GetActiveDirectoryClient();
user = (User)await client.Users.GetByObjectId(objectId).ExecuteAsync();
user.SetExtendedProperty("extension_e99c8a1afb544da491b098931b0a2ad8_LaidentidadSecreta", "Bruno Días");
await user.UpdateAsync();
获取值:
string extensionValue = (String)user.GetExtendedProperties()["extension_e99c8a1afb544da491b098931b0a2ad8_LaidentidadSecreta"];// The Guid is the App Guid.
当心这个!!!!幸运的是,如果您初始化 ActiveDirectoryClient 并使用 Microsoft.Data.Services.Client 版本 5.7.0 将起作用。
//////
private static void UndeclaredPropertyHandler(MessageWriterSettingsArgs args)
{
var field = args.Settings.GetType().GetField("settings",
BindingFlags.NonPublic | BindingFlags.Instance);
var settingsObject = field?.GetValue(args.Settings);
var settings = settingsObject as ODataMessageWriterSettings;
if (settings != null)
{
settings.UndeclaredPropertyBehaviorKinds =
ODataUndeclaredPropertyBehaviorKinds.SupportUndeclaredValueProperty;
}
}
public async Task<ActionResult> SomeMethod(string objectId)
{
User user = null;
try
{
// forma normal
ActiveDirectoryClient client = AuthenticationHelper.GetActiveDirectoryClient();
// #adicional
client.Context
.Configurations.RequestPipeline
.OnMessageWriterSettingsCreated(UndeclaredPropertyHandler);
// #adicional