Код: Выделить всё
@using System.Security.Principal
@using System.DirectoryServices;
@using System.DirectoryServices.AccountManagement;
@using System.DirectoryServices.ActiveDirectory;
@inject AuthenticationStateProvider asp
@attribute [Authorize]
... html here ...
@code {
protected override async Task OnInitializedAsync()
{
var authState = await asp.GetAuthenticationStateAsync();
var user = authState.User;
var windowsIdentity = (WindowsIdentity)User.Identity;
WindowsIdentity.RunImpersonated(windowsIdentity.AccessToken, () =>
{
var principalContext = new PrincipalContext(ContextType.Domain);
// Find by samAccountName
var adUser = UserPrincipalEx.FindByIdentity(principalContext, "different.account");
adUser.Description = "Employee description goes here";
adUser.Save();
}
}
}
< /code>
Когда я запускаю этот код на своей машине с Visual Studio и IIS Express, он работает нормально. Свойство устанавливается в AD, а в журнале аудита правильно показывается, что моя учетная запись внесла изменение на пользователь «разные.account».
У меня не было блока .Runimpersonated
system.directoryservices.accountmanagement.principaloperationexception: произошла ошибка операций < /p>
< /blockquote>
Подробнее здесь: https://stackoverflow.com/questions/797 ... tory-fails