Microsoft Graph - 资源所有者密码凭据替代
Microsoft Graph - Resource Owner Password Credentials alternative
上下文如下:
我有一个包含用户的外部应用程序(我们称之为 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 only supports Delegated permission, which means you have to follow Get access on behalf of a user 获取访问令牌。
所以如果你不想使用ROPC流程,你需要在你的项目中集成AAD授权登录。
请按照此 document 了解操作方法。
上下文如下:
我有一个包含用户的外部应用程序(我们称之为 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 only supports Delegated permission, which means you have to follow Get access on behalf of a user 获取访问令牌。
所以如果你不想使用ROPC流程,你需要在你的项目中集成AAD授权登录。
请按照此 document 了解操作方法。