Microsoft Graph - 资源所有者密码凭据替代

Microsoft Graph - Resource Owner Password Credentials alternative

上下文如下:

场景如下:

这是问题所在:

这是我的丑陋修复:

    public async Task<string> GetTokenUser(string email, string password) 
    {
        string token = null;
        var clientID = "<ApplicationClientID>";
        var secret = "<ApplicationSecret>";
        var tenantID = HttpUtility.UrlEncode("<TenantDomain>");
        var resource = HttpUtility.UrlEncode("https://graph.microsoft.com");
        email= HttpUtility.UrlEncode(email);
        password= HttpUtility.UrlEncode(password);

        using (HttpClient client = new HttpClient())
        {
            var tokenEndpoint = @"https://login.windows.net/" + tenantID + "/oauth2/token";
            var accept = "application/json";

            client.DefaultRequestHeaders.Add("Accept", accept);
            string postBody = @"resource=" + resource + @"
                                &client_id=" + clientID + @"
                                &client_secret=" + secret  + @"
                                &grant_type=password
                                &username=" + email + @"
                                &password=" + password + "&scope=openid";

            using (var response = await client.PostAsync(tokenEndpoint, new StringContent(postBody, Encoding.UTF8, "application/x-www-form-urlencoded")))
            {
                if (response.IsSuccessStatusCode)
                {
                    var jsonresult = JObject.Parse(await response.Content.ReadAsStringAsync());
                    token = (string)jsonresult["access_token"];
                }
            }
        }

        return token;
    }

这是我需要的:

重要说明:我对如何实现这一点完全没有偏好,只要它有效且可靠即可。

编辑 1:代码中的错字

如果要访问 Microsoft Graph,则需要 Azure AD 身份验证。

Create onlineMeeting only supports Delegated permission, which means you have to follow Get access on behalf of a user 获取访问令牌。

所以如果你不想使用ROPC流程,你需要在你的项目中集成AAD授权登录。

请按照此 document 了解操作方法。