【问题标题】:Microsoft Graph - Resource Owner Password Credentials alternativeMicrosoft Graph - 资源所有者密码凭据替代
【发布时间】:2020-09-17 21:18:51
【问题描述】:

这是上下文:

  • 我有一个包含用户的外部应用程序(我们称之为 EA)。每个用户都可以使用电子邮件和密码登录。

  • 我有一个包含用户的 Azure Active Directory。他们与 EA 中的用户完全相同。

这是场景:

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

问题来了:

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

这是我的丑陋修复:

  • 我使用资源所有者密码凭据在后端再次登录用户。我这样做的方式如下:当用户尝试创建在线会议时,系统会提示他再次以 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;
    }

这是我需要的:

  • 我需要找到一种更好的方式让用户在 Azure Active Directory 中登录,而无需再次提示密码。有什么方法可以告诉 Azure Active Directory X 用户已通过电子邮件登录,而无需发送纯文本密码?

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

编辑 1:代码中的错字

【问题讨论】:

    标签: c# oauth-2.0 asp.net-mvc-5 azure-active-directory microsoft-graph-api


    【解决方案1】:

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

    Create onlineMeeting只支持Delegated权限,也就是说你必须关注Get access on behalf of a user才能获得访问令牌。

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

    请关注此document 了解如何操作。

    【讨论】:

    • 非常感谢。我会检查一下并在这个星期一回复你。
    • 我检查了链接,它们肯定很有帮助。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2017-11-14
    • 2018-05-25
    • 2021-06-20
    • 2015-01-06
    • 2021-12-05
    • 2015-05-20
    • 2013-11-23
    • 2020-02-29
    相关资源
    最近更新 更多