c# - Microsoft Graph - 资源所有者密码凭据替代
问题描述
这是上下文:
我有一个包含用户的外部应用程序(我们称之为 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:代码中的错字
解决方案
如果要访问 Microsoft Graph,则需要 Azure AD 身份验证。
Create onlineMeeting仅支持 Delegated 权限,这意味着您必须按照代表用户获取访问权限才能获取访问令牌。
所以如果你不想使用 ROPC 流程,你需要将 AAD 授权登录集成到你的项目中。
请按照本文档了解如何操作。
推荐阅读
- xamarin.forms - Xamarin 形成 ios sdk 版本不匹配问题
- docker - 从 Docker 运行 Jupyter Notebook:curl: (56) Recv failure: Connection reset by peer
- macos - 如何在 macOS 中将水印添加到屏幕(NSScreen)
- php - 我如何在laravel中放置休假计数器?
- sql - 使用分区计算 SQL 中的进程重复次数
- java - RC4 整数加密/解密 Java
- python - python将多个excel中的所有工作表附加到pandas数据框中的有效方法
- node.js - 在将标头发送到客户端后无法设置标头,(但在重新设置标头的位置不明显)
- android - 从后台打开应用程序时,片段中的可序列化 ViewPager 导致 Android 应用程序崩溃
- cassandra - 如何将用户定义的数据类型列表索引为冻结在表“列表”
>”?