C# 需要使用C rest调用生成Azure AD承载令牌

C# 需要使用C rest调用生成Azure AD承载令牌,c#,azure,azure-active-directory,bearer-token,azure-security,C#,Azure,Azure Active Directory,Bearer Token,Azure Security,我想通过C REST调用生成Azure AD承载令牌。我想在API调用中编写用户注册逻辑。我将此用作令牌端点: 我遵循了文章中描述的相同过程 我使用的用户凭据是全局管理员 但我仍然得到'未经授权'的错误。请在下面找到代码段和响应正文- 代码 另外,请指出是否有其他方法可以在不进行REST呼叫的情况下添加新用户。我不想在客户端应用程序中注册用户 [更新]请查找添加权限和角色的屏幕截图。 正如Tom提到的,您的问题应该是由于用户没有权限访问您的应用程序造成的 由于您正在使用ROPC授权流,请确保该用

我想通过C REST调用生成Azure AD承载令牌。我想在API调用中编写用户注册逻辑。我将此用作令牌端点:

我遵循了文章中描述的相同过程

我使用的用户凭据是全局管理员

但我仍然得到'未经授权'的错误。请在下面找到代码段和响应正文-

代码

另外,请指出是否有其他方法可以在不进行REST呼叫的情况下添加新用户。我不想在客户端应用程序中注册用户

[更新]请查找添加权限和角色的屏幕截图。

正如Tom提到的,您的问题应该是由于用户没有权限访问您的应用程序造成的


由于您正在使用ROPC授权流,请确保该用户是该客户端/AAD应用程序的所有者

您可以将onwer添加到AAD应用程序:

转到Azure门户>Azure Acitve目录>应用程序注册>应用程序>设置>所有者>添加所有者>选择该用户

此外,如果用户已启用MFA,则此流将不起作用

如果有帮助,请告诉我

另外,请指出是否有任何其他方式添加新用户而不进行REST调用。我不想在客户端应用程序中注册用户

如果要创建用户,我们可以使用

在那之前,我们需要。并添加Microsoft graph权限。有关更多详细信息,请参阅

我使用Directory.ReadWrite.All权限测试它。别忘了授予权限

演示代码

string graphResourceId = "https://graph.microsoft.com/";
string authority = "https://login.microsoftonline.com/{0}";
string tenantId = "tenant Id";
string clientId = "client Id";
string secret = "secret";
authority = String.Format(authority, tenantId);
AuthenticationContext authContext = new AuthenticationContext(authority);
var accessToken = authContext.AcquireTokenAsync(graphResourceId, new ClientCredential(clientId, secret)).Result.AccessToken;
            var graphserviceClient = new GraphServiceClient(
                new DelegateAuthenticationProvider(
                    requestMessage =>
                    {
                        requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);

                        return Task.FromResult(0);
                    }));
var user = new User
         {
             UserPrincipalName = "tomaccount1@xxxxx.onmicrosoft.com",
             AccountEnabled = true,
             DisplayName = "tom1",
             PasswordProfile = new PasswordProfile
             {
                 ForceChangePasswordNextSignIn = true,
                 Password = "1234qweA!@#$%6"
             },
              MailNickname = "tomaccount1"
            };
 var addUserResult = graphserviceClient.Users.Request().AddAsync(user).Result;
测试结果:

查看Azure广告

Packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.Graph" version="1.9.0" targetFramework="net471" />
  <package id="Microsoft.Graph.Core" version="1.9.0" targetFramework="net471" />
  <package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.19.4" targetFramework="net471" />
  <package id="Newtonsoft.Json" version="11.0.2" targetFramework="net471" />
  <package id="System.IO" version="4.3.0" targetFramework="net471" />
  <package id="System.Net.Http" version="4.3.3" targetFramework="net471" />
  <package id="System.Runtime" version="4.3.0" targetFramework="net471" />
  <package id="System.Security.Cryptography.Algorithms" version="4.3.1" targetFramework="net471" />
  <package id="System.Security.Cryptography.Encoding" version="4.3.0" targetFramework="net471" />
  <package id="System.Security.Cryptography.Primitives" version="4.3.0" targetFramework="net471" />
  <package id="System.Security.Cryptography.X509Certificates" version="4.3.2" targetFramework="net471" />
</packages>

不使用?401错误的任何特殊原因?这意味着您无权这样做。您要访问哪个API?您提到的是操作Microsoft Graph API。对于不同的API,资源是不同的。有关要操作哪个API的更多信息将更有帮助。由于您使用的是ROPC授权流,该用户是该clientAAD应用程序的所有者吗?我正在尝试使用Graph API添加新用户。请检查更新的屏幕截图以了解添加的角色和权限。对。应改为login.microsoftonline.com。单击中的“授予同意”按钮。我看到您正在使用应用程序权限,我认为密码流不需要这些权限,很可能需要授权权限。要使用应用权限,您需要切换到客户端凭据流应用id和应用机密。这在ADAL.Hi@Wayne中得到了充分的支持,我已经添加了这个用户作为应用程序的所有者。但它仍然给出同样的错误。请查找更新的屏幕截图以供参考。Hi@Sudip。你的应用程序是什么类型的?我正在用控制台应用程序.NET 4.6进行测试。稍后我将把代码移动到Web API逻辑。嗯。。。你最好用本机应用注册你的应用。好的。。我会试试的。我会随时通知你的。谢谢,不过现在有点忙。我一定会试试这个,并随时通知你。
string graphResourceId = "https://graph.microsoft.com/";
string authority = "https://login.microsoftonline.com/{0}";
string tenantId = "tenant Id";
string clientId = "client Id";
string secret = "secret";
authority = String.Format(authority, tenantId);
AuthenticationContext authContext = new AuthenticationContext(authority);
var accessToken = authContext.AcquireTokenAsync(graphResourceId, new ClientCredential(clientId, secret)).Result.AccessToken;
            var graphserviceClient = new GraphServiceClient(
                new DelegateAuthenticationProvider(
                    requestMessage =>
                    {
                        requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);

                        return Task.FromResult(0);
                    }));
var user = new User
         {
             UserPrincipalName = "tomaccount1@xxxxx.onmicrosoft.com",
             AccountEnabled = true,
             DisplayName = "tom1",
             PasswordProfile = new PasswordProfile
             {
                 ForceChangePasswordNextSignIn = true,
                 Password = "1234qweA!@#$%6"
             },
              MailNickname = "tomaccount1"
            };
 var addUserResult = graphserviceClient.Users.Request().AddAsync(user).Result;
<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.Graph" version="1.9.0" targetFramework="net471" />
  <package id="Microsoft.Graph.Core" version="1.9.0" targetFramework="net471" />
  <package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.19.4" targetFramework="net471" />
  <package id="Newtonsoft.Json" version="11.0.2" targetFramework="net471" />
  <package id="System.IO" version="4.3.0" targetFramework="net471" />
  <package id="System.Net.Http" version="4.3.3" targetFramework="net471" />
  <package id="System.Runtime" version="4.3.0" targetFramework="net471" />
  <package id="System.Security.Cryptography.Algorithms" version="4.3.1" targetFramework="net471" />
  <package id="System.Security.Cryptography.Encoding" version="4.3.0" targetFramework="net471" />
  <package id="System.Security.Cryptography.Primitives" version="4.3.0" targetFramework="net471" />
  <package id="System.Security.Cryptography.X509Certificates" version="4.3.2" targetFramework="net471" />
</packages>