C# Microsoft图形-资源所有者密码凭据替代方案

C# Microsoft图形-资源所有者密码凭据替代方案,c#,oauth-2.0,asp.net-mvc-5,azure-active-directory,microsoft-graph-api,C#,Oauth 2.0,Asp.net Mvc 5,Azure Active Directory,Microsoft Graph Api,以下是上下文: 我有一个包含用户的外部应用程序(我们称之为EA)。每个用户都可以使用电子邮件和密码登录 我有一个包含用户的Azure Active Directory。他们与EA中的用户完全相同 以下是场景: 用户在EA中登录。在EA中,用户可以使用Graph API创建一个在线Microsoft团队会议(会议创建已经完成并且正在工作) 问题是: 用户需要再次手动登录Azure Active Directory以创建联机Microsoft团队会议,即使他已登录EA 这是我丑陋的修

以下是上下文:

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

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

以下是场景:

  • 用户在EA中登录。在EA中,用户可以使用Graph API创建一个在线Microsoft团队会议(会议创建已经完成并且正在工作)
问题是:

  • 用户需要再次手动登录Azure Active Directory以创建联机Microsoft团队会议,即使他已登录EA
这是我丑陋的修正:

  • 我使用资源所有者密码凭据在后端再次登录用户。我这样做的方式如下:当用户尝试创建在线会议时,系统会提示他以HTML形式再次输入is密码。表单最终导致此方法,恢复Azure Active Directory访问令牌:
公共异步任务GetTokenUser(字符串电子邮件、字符串密码)
{
字符串标记=null;
var clientID=“”;
var secret=“”;
var tenantID=HttpUtility.UrlEncode(“”);
var resource=HttpUtility.UrlEncode(“https://graph.microsoft.com");
email=HttpUtility.UrlEncode(电子邮件);
password=HttpUtility.UrlEncode(密码);
使用(HttpClient=new HttpClient())
{
var tokenpoint=@”https://login.windows.net/“+tenantID+”/oauth2/token”;
var accept=“application/json”;
client.DefaultRequestHeaders.Add(“接受”,接受);
字符串postBody=@“resource=“+resource+@”
&客户端id=“+clientID+@”
&客户端_secret=“+secret+@”
&授权类型=密码
&username=“+电子邮件+@”
&password=“+password+”&scope=openid”;
使用(var response=wait client.PostAsync(令牌端点,新的StringContent(postBody,Encoding.UTF8,“application/x-www-form-urlencoded”))
{
if(响应。IsSuccessStatusCode)
{
var jsonresult=JObject.Parse(wait response.Content.ReadAsStringAsync());
令牌=(字符串)jsonresult[“访问令牌”];
}
}
}
返回令牌;
}
我需要的是:

  • 我需要找到一种更好的方法来登录Azure Active Directory中的用户,而无需再次提示密码。有没有办法告诉Azure Active Directory X用户通过电子邮件登录,而不必发送纯文本密码
重要提示:我绝对不喜欢如何实现这一目标,只要它有效且可靠


编辑1:如果要访问Microsoft Graph,则必须键入代码

Azure广告验证

仅支持委派权限,这意味着您必须遵循才能获得访问令牌

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


请按照此操作学习如何操作。

如果您想访问Microsoft Graph,Azure AD身份验证是必要的

仅支持委派权限,这意味着您必须遵循才能获得访问令牌

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


请按此学习如何操作。

非常感谢。我会检查一下,并在本周一给你回复。我检查了链接,它们肯定很有帮助。非常感谢。非常感谢你。我会检查一下,并在本周一给你回复。我检查了链接,它们肯定很有帮助。非常感谢。
    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;
    }