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

问题描述

这里是上下文:

这是场景:

  • 用户在EA中的登录名。在EA中,用户可以使用Graph API创建在线Microsoft Teams会议(会议创建已经完成并且可以进行)。

问题出在这里

  • 即使用户登录EA,也需要再次在Azure Active Directory中手动登录才能创建在线Microsoft Teams会议。

这是我的丑陋解决方法

  • 我使用了资源所有者密码凭据再次在后端登录用户。我的操作方式如下:当用户尝试创建在线会议时,系统提示他再次以HTML形式输入密码。该表单最终导致此方法恢复了Azure Active Directory访问令牌:
    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仅支持委派权限,这意味着您必须遵循Get access on behalf of a user才能获得访问令牌。

因此,如果您不想使用ROPC流,则需要将AAD授权登录集成到您的项目中。

请按照此document进行操作。