首页 > 解决方案 > 基本身份验证停用 MS Graph

问题描述

有人可以告诉我,在 2020 年 10 月 13 日之后是否仍然可以使用 MS Graph 的以下身份验证方法?

public static async Task<string> GetUserAccessTokenAsync()
{
    String APIToken = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX";
    String LoginMail = "xxx@xxx.be";
    String LoginWachtwoord = "xxxxxxxxxxx";

    UserPasswordCredential userPasswordCredential = new UserPasswordCredential(LoginMail, LoginWachtwoord);

    AuthenticationContext authContext = new AuthenticationContext("https://login.microsoftonline.com/diekeure.be");

    //Console App for Microsoft Graph
    AuthenticationResult token = await authContext.AcquireTokenAsync("https://graph.microsoft.com/", APIToken, userPasswordCredential);

    return token.AccessToken;
} /* GetUserAccessTokenAsync */

public static GraphServiceClient GetAuthenticatedClient()
{
    GraphServiceClient graphClient = new GraphServiceClient(
        new DelegateAuthenticationProvider(
            async (requestMessage) => {
                string accessToken = await GetUserAccessTokenAsync();

                        // Append the access token to the request.
                        requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
            }));
    return graphClient;
} /* GetAuthenticatedClient */

-> await graphClient....

Nuget 包:Microsoft.IdentityModel.Clients.ActiveDirectory v3.19.8

我也不确定这是否行不通

标签: c#microsoft-graph-apiexchange-basicauth

解决方案


您的示例未使用基本身份验证,而是使用 OAuth 密码授权。虽然不是一种非常安全的身份验证机制(因为您必须存储密码),但它与基本身份验证完全不同。

您引用的电子邮件也特定于Exchange Web 服务 (EWS) 等遗留服务。Microsoft Graph不是旧版 API,它是 Microsoft 服务的推荐/现代 REST API。


推荐阅读