UserPrincipal.Current返回GroupPrincipal(当向Azure AD创建的用户运行时) - 但是期望UserPrincipal类型

时间:2017-07-27 08:27:16

标签: azure-active-directory userprincipal groupprincipal

我正在尝试从Azure AD托管用户帐户中检索以下信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.DirectoryServices.AccountManagement;

namespace UserName_API_functionCall
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("User Principal parameter values");
            UserPrincipal current = UserPrincipal.Current;
            Console.WriteLine("User email: {0}", current.EmailAddress);
            Console.WriteLine("User email: {0}", current.MiddleName);
            Console.WriteLine("User email: {0}", current.GivenName);
            Console.WriteLine("User email: {0}", current.Surname);
            Console.ReadLine();
        }
    }
}

然而,UserPrincipal.Current方法调用失败并出现异常,因为它(当用户连接Azure AD时)返回GroupPrincipal数据类型而不是UserPrincipal数据类型(没有强制转换能力)

如何最有效地从本地填充的属性中检索“当前用户”信息?

由于

0 个答案:

没有答案