检索访问令牌

To retrieve access token

我创建了一个 MVC 应用程序来将工作上报给组织内的其他人。我已将组织中的所有成员添加到 AAD, 并在那里注册了一个应用程序,创建了应用程序服务并将该应用程序服务链接到已启用 SSO 的已注册应用程序。

现在每次有人访问该应用程序时,他们都可以使用各自的凭据成功登录。

我想知道的是检索我的 AAD 中的所有成员并将它们显示在下拉列表中,这样任何人都可以通过查看下拉列表来升级给其他人。

我已尝试使用示例图 SDK 来获取我组织中的用户名 使用此代码

    private string redirectUri = ConfigurationManager.AppSettings["ida:RedirectUri"];
    private string appId = ConfigurationManager.AppSettings["ida:AppId"];
    private string appSecret = ConfigurationManager.AppSettings["ida:AppSecret"];
    private string scopes = ConfigurationManager.AppSettings["ida:GraphScopes"];

public async Task<string> GetUserAccessTokenAsync()
    {
        string signedInUserID = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
        HttpContextWrapper httpContext = new HttpContextWrapper(HttpContext.Current);
        TokenCache userTokenCache = new SessionTokenCache(signedInUserID, httpContext).GetMsalCacheInstance();
        //var cachedItems = tokenCache.ReadItems(appId); // see what's in the cache

        ConfidentialClientApplication cca = new ConfidentialClientApplication(
            appId, 
            redirectUri,
            new ClientCredential(appSecret),
            userTokenCache,
            null);

        try
        {
            AuthenticationResult result = await cca.AcquireTokenSilentAsync(scopes.Split(new char[] { ' ' }), cca.Users.First());
            return result.AccessToken;
        }

        // Unable to retrieve the access token silently.
        catch (Exception)
        {
            HttpContext.Current.Request.GetOwinContext().Authentication.Challenge(
                new AuthenticationProperties() { RedirectUri = "/" },
                OpenIdConnectAuthenticationDefaults.AuthenticationType);

            throw new ServiceException(
                new Error
                {
                    Code = GraphErrorCode.AuthenticationFailure.ToString(),
                    Message = Resource.Error_AuthChallengeNeeded,
                });
        }
    }

范围有所变化。

<add key="ida:AppId" value="xxxxx-xxxxxxx-xxxxxx-xxxxxxx"/>
<add key="ida:AppSecret" value="xxxxxxxxxxx"/>
<add key="ida:RedirectUri" value="http://localhost:55065/"/>
<add key="ida:GraphScopes" value="User.ReadBasic.All User.Read Mail.Send Files.ReadWrite"/>

这使我能够获得组织中所有用户的基本详细信息。 但是我如何在我的应用程序中实现这一点,其中身份验证相关的东西仅在 azure 中完成,并且整个解决方案中没有用于身份验证和授权的代码。

谢谢

Subham, NATHCORP, 印度

But how I can achieve this in my app where authentication related stuffs are done in azure only, and there is no code for authentication and authorization in entire solution.

根据我的理解,您正在使用 build-in 功能 App Service Authentication / Authorization. You could follow here 将您的 Web 应用配置为使用 AAD 登录。并且您需要为您的 AD 应用配置所需的权限,如下所示:

注意:对于Azure AD graph,需要为WindowsAzure Active Directory设置相关权限API。对于 Microsoft Graph,您需要配置 Microsoft Graph API.

然后,您需要为您的网络应用配置其他设置。您可以访问 https://resources.azure.com/,选择您的 Web 应用程序并按如下方式更新应用程序服务授权配置:

注意:要使用 Microsoft Graph API,您需要将 resource 设置为 https://graph.microsoft.com。详情可以关注here.

要在您的应用程序中检索访问令牌,您可以从请求 header X-MS-TOKEN-AAD-ACCESS-TOKEN 中获取它。详情可以关注Working with user identities in your application.

此外,您可以使用相关的访问令牌为 Microsoft Graph API 使用 Microsoft.Azure.ActiveDirectory.GraphClient package for Microsoft Azure Active Directory Graph API, Microsoft.Graph 包。