oauth2.0 Azure AD异常Microsoft.Identity.Client.MsalServiceException:为输入参数“scope”提供的值无效

n1bvdmb6  于 2023-10-15  发布在  其他
关注(0)|答案(1)|浏览(98)

我创建Azure广告多租户应用程序,以允许组织和公共电子邮件。目前正在.net控制台应用程序上测试。

var tenantId = "d0748657 ........";
 var clientId = "23a9c7a6 ........";
 var authorityUri = $"https://login.microsoftonline.com/common";     
 var redirectUri = "http://localhost";

 var pca = PublicClientApplicationBuilder
          .Create(clientId)
          .WithAuthority(new Uri(authorityUri))
          .WithRedirectUri(redirectUri)
          .Build();

现在谈到smtpScope,我使用https://outlook.office365.com/SMTP.Send

var smtpScope = new string[] { "https://outlook.office365.com/SMTP.Send",  }; 
            var accounts = await pca.GetAccountsAsync();
            AuthenticationResult result = null;
            if (accounts.Any())          
                result = await pca.AcquireTokenSilent(smtpScope, accounts.FirstOrDefault()).ExecuteAsync();
            
            else
            {
                try
                {
                    result = await pca.AcquireTokenInteractive(smtpScope).ExecuteAsync();
                }
                catch (MsalException ex)
                {
                    Console.WriteLine($"Error acquiring access token: {ex}");
                }

                Console.WriteLine($"Token : {result.AccessToken}");

当我使用我的组织电子邮件登录时,我会获得令牌,一切正常,但当我使用个人Outlook帐户登录时,我会收到此错误
获取访问令牌时出错:MSAL.Desktop.4.56.0.0.MsalServiceException:错误代码:invalid_scope Microsoft.Identity.Client.MsalServiceException:为输入参数“scope”提供的资源值无效
我错过了什么?smtpScope是否不正确?

lmvvr0a8

lmvvr0a81#

当我在我的环境中运行你的代码,我也得到了相同的错误与个人Outlook帐户:

var tenantId = "d0748657 ........";
 var clientId = "23a9c7a6 ........";
 var authorityUri = $"https://login.microsoftonline.com/common";     
 var redirectUri = "http://localhost";

 var pca = PublicClientApplicationBuilder
          .Create(clientId)
          .WithAuthority(new Uri(authorityUri))
          .WithRedirectUri(redirectUri)
          .Build();

 var smtpScope = new string[] { "https://outlook.office365.com/SMTP.Send",  }; 
            var accounts = await pca.GetAccountsAsync();
            AuthenticationResult result = null;
            if (accounts.Any())          
                result = await pca.AcquireTokenSilent(smtpScope, accounts.FirstOrDefault()).ExecuteAsync();
            
            else
            {
                try
                {
                    result = await pca.AcquireTokenInteractive(smtpScope).ExecuteAsync();
                }
                catch (MsalException ex)
                {
                    Console.WriteLine($"Error acquiring access token: {ex}");
                }

                Console.WriteLine($"Token : {result.AccessToken}");

回复:

AFAIK,SMTP.Send权限目前仅适用于工作学校(组织)帐户,而不是2023年2月之后的个人Microsoft帐户,以生成OAuth2访问令牌。
当我用组织账号再次运行同样的代码时,我在响应中成功获得token,如下所示:

为了确认,我在jwt.ms中解码了令牌,其中**audscp**声明如下:

或者,您可以通过添加Mail.Send切换到Microsoft Graph API,从个人Outlook帐户发送邮件。为此,通过替换scopevalue来修改代码:

using Microsoft.Identity.Client;

var clientId = "appId";
var authorityUri = $"https://login.microsoftonline.com/common";
var redirectUri = "http://localhost";

var pca = PublicClientApplicationBuilder
         .Create(clientId)
         .WithAuthority(new Uri(authorityUri))
         .WithRedirectUri(redirectUri)
         .Build();

var graphScopes = new string[] { "https://graph.microsoft.com/Mail.Send" };

var accounts = await pca.GetAccountsAsync();
AuthenticationResult result = null;

if (accounts.Any())
{
    result = await pca.AcquireTokenSilent(graphScopes, accounts.FirstOrDefault()).ExecuteAsync();
}
else
{
    try
    {
        result = await pca.AcquireTokenInteractive(graphScopes).ExecuteAsync();
    }
    catch (MsalException ex)
    {
        Console.WriteLine($"Error acquiring access token: {ex}");
    }

    Console.WriteLine($"Token : {result.AccessToken}");
}

当我通过运行上述代码使用个人outlook帐户登录时,我得到了这样的同意提示:

在接受上述同意后,我在控制台成功获得了访问令牌,如下所示:

参考:SMTP.Send OAuth permission not working for consumer accounts - Microsoft Q&A by Akshay-MSFT

相关问题