Open Identifier opened 3 years ago
@Identifier thanks for logging the issue, this seems like an issue and we should have an if check instead of a hard cast there.
Root of the case is not the cast. In the OP example we explicitly ask UserPrincipal but the method return something else.
After some investigations I believe AccountManagement API doesn't support AzureAD/Entra accounts at all. What would ContentType
be for AzureAD/Entra accounts?
Duplicate #30408
In a .NET 5 Console app as follows:
UserPrincipal.Current calls UserPrincipal.FindByIdentity, which throws an InvalidCastException when trying to cast a GroupPrincipal to a UserPrincipal:
The specific line is here:
https://github.com/dotnet/runtime/blob/4ab71ae44b78a6139739798f8bba090d520bfc1f/src/libraries/System.DirectoryServices.AccountManagement/src/System/DirectoryServices/AccountManagement/User.cs#L271
Apparently FindByIdentityWithType is returning a GroupPrincipal even though we wanted a UserPrincipal. This is running on a Windows 10 machine joined to Microsoft Azure AD (normal MSFT employee setup).