我正在用azuread创建一个c#Web应用程序 . 该应用程序将允许Azure广告管理员管理租户的用户 . 为此,我需要来自登录用户的租户的特定拨款 .

我得到的代码适用于大多数范围,如'user.readwrite'和'directory.readwrite.all' . 但是,当我添加范围'User.ReadWrite.All'时,我收到以下错误:

AADSTS65001:用户或管理员未同意使用ID为“客户端应用程序ID”的应用程序 . 发送此用户和资源的交互式授权请求 .

有问题的用户是来自https://apps.dev.microsoft.com/的应用程序的所有者 . 在此应用程序中,我添加了范围:Mail.Send,User.Read,User.ReadWrite,Directory.ReadWrite.All和User.ReadWrite.All . 对于最后两个范围,需要管理员同意 .

但要强制管理员同意,即使用户是所有者 . 我使用此URL为我的管理员和租户中的所有用户同意上述范围的应用程序 .

login.microsoftonline.com/common/adminconsent?client_id=Client application Id&state = adminconsentstate&redirect_uri =我的本地webapp的uri

在Web应用程序中,我使用Microsoft endpoints v2.0与Azuread连接 .

login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=Client application Id&response_mode = form_post&response_type = code id_token&scope = openid email profile offline_access&state = OpenIdConnect.AuthenticationProperties%3d ...&prompt = select_account&redirect_uri = Redirect Url&post_logout_redirect_uri = Logout Redirect网址

我使用以下代码来获取Accesstoken .

string userObjectId = "User Object Id Guid";
HttpContextBase baseContext = context.OwinContext.Environment["System.Web.HttpContextBase"] as HttpContextBase;
var tokenCache = new SessionTokenCacheMSAL(userObjectId,baseContext);

ConfidentialClientApplication cca = new ConfidentialClientApplication(_applicationSettings.ClientId,redirectUri.ToString(),new ClientCredential(_applicationSettings.AppKey), tokenCache);

var scopes = new List<string>();
scopes.Add("User.Read");
scopes.Add("user.readbasic.all");
scopes.Add("User.ReadWrite.All");
scopes.Add("Directory.ReadWrite.All");

var runTaskAcquireToken = Task.Run(async () => await cca.AcquireTokenByAuthorizationCodeAsync(scopes.ToArray(), context.Code));
runTaskAcquireToken.Wait();
var result = runtask.Result;

当请求使用Accesstoken和范围'User.ReadWrite.All'时,我得到上面的错误'AADSTS65001' . 当我删除范围'User.ReadWrite.All'时,我可以查看用户,获取DirectoryRoles和组织/租户 .

奇怪的是,“Directory.ReadWrite.All”范围也需要与Microsoft提供的“User.ReadWrite.All”相同的管理员同意 .

资源:

http://graph.microsoft.io/en-us/docs/authorization/permission_scopes

任何帮助解决我的问题将非常感谢 .

谢谢 .