本机应用程序和 Azure AD - Hello World

Native App and Azure AD - Hello World

我正在尝试创建一个需要有效用户的控制台应用程序的简单示例。该应用程序是一个简单的控制台应用程序:

static void Main(string[] args)
{
    string authority = "https://login.windows.net/----------------.onmicrosoft.com";
    AuthenticationContext context = new AuthenticationContext(authority);
    Console.WriteLine("Context created");

    string resource = "ConsoleApp1";
    string clientId = "------------------------------------";
    string redirectUri = "http://localhost";

    var parameters = new PlatformParameters(PromptBehavior.RefreshSession);

    context.TokenCache.Clear();


    // Throws AggregateException--> AADSTS50105: The signed in user is not assigned to a role for the application
    var token = Task.Run(() => context.AcquireTokenAsync(resource, clientId, new Uri(redirectUri), parameters)).Result;

    Console.WriteLine("Hello, authorized user");
}

应用程序启动,呈现登录window,然后抛出异常

AADSTS50105: The signed in user is not assigned to a role for the application

在 Azure 门户中,我已将 ConsoleApp1 注册为本机应用程序。

必需的权限下,我勾选了:

除此之外,我很困惑,错误消息告诉我需要为用户分配某种角色,但我无法在应用程序设置中的任何位置找到它。有人可以帮忙解释一下吗?

您指定了错误的资源(ConsoleApp1)。资源应代表您已授予此应用程序的资源以及您为其获取访问令牌的资源。

您似乎已将 Azure AD Graph 的权限授予此应用程序,是否要获取访问令牌以使用 Azure Graph REST?

如果我没理解错的话,你可以用https://graph.windows.net替换资源。

更多关于与Azure AD集成的详细信息,您可以参考here