Skip to content

UserPrincipal.Current throws InvalidCastException #38411

@orinem

Description

@orinem

Description

See also #30408

If logged on as an Azure AD user, UserPrincipal.Current throws an InvalidCastException.

Steps to reproduce:

Connect a non-domain connected Windows 10 computer to an Azure Active Directory:

Settings App
Select Accounts
Select "Access work or school"
Click "Connect" and follow the steps.

When it completes, restart and log in as the Azure AD user. Run the code below.

Code

using System;
using System.DirectoryServices.AccountManagement;

namespace TestDotNetCore
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var principal = UserPrincipal.Current;
                Console.WriteLine(principal.Name);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}

It prints:

Unable to cast object of type 'System.DirectoryServices.AccountManagement.GroupPrincipal' to type 'System.DirectoryServices.AccountManagement.UserPrincipal'.

Configuration

Tried with the following: x86, .net Framework 4.5.2, 4.7.2, Core 3.1.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions