c# - 域名无效 Azure Active Directory 问题和迁移到 Microsoft Graph C# 应用程序

标签 c# azure azure-active-directory microsoft-graph-api

我正在尝试运行以下代码以获取用户详细信息作为来自 Azure Active Directory 的响应:

using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace AADConsole2
{
    class Program
    {

        private const string aadInstance = "https://login.microsoftonline.com/{0}";
        private const string resource= "https://graph.windows.net";
        private const string GraphServiceObjectId = "XXX";
        private const string TenantId = "XXXXX";
        private const string tenant = "company.onmicrosoft.com";
        private const string ClientId = "XXXX";
        private static string appKey= "XXXXXXXXXXXXXXXX";
        static string authority = String.Format(System.Globalization.CultureInfo.InvariantCulture, aadInstance, tenant);

        private static HttpClient httpclient = new HttpClient();
        private static AuthenticationContext context = null;
        private static ClientCredential credential = null;

        static void Main(string[] args)
        {

            context = new AuthenticationContext(authority);
            credential = new ClientCredential(ClientId, appKey);
            Task<string> token = GetToken();
            token.Wait();
            Console.WriteLine(token.Result);
            Task<string> users = GetUsers(token.Result);
            users.Wait();
            Console.WriteLine(users.Result);
            Console.ReadLine();
        }

        private static async Task<string> GetUsers(string result) {
            //throw new NotImplementedException();
            string users = null;
            string queryString = "test";

var uri = "https://graph.windows.net/ {your_tenant_name}.onmicrosoft.com/users?api-version=1.6";

            httpclient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result);
            var getResult = await httpclient.GetAsync(uri);

            if(getResult.Content != null)
            {
                users = await getResult.Content.ReadAsStringAsync();

            }
            return users;

        }


        private static async Task<string> GetToken()
        {
            AuthenticationResult result = null;
            string token = null;
            result = await context.AcquireTokenAsync(resource, credential);
            token = result.AccessToken;
            return token;



        }
    }
}

我收到以下错误:

uMCJ9.FdttazjoKYZWP_SmC5B7Nd3kOF-jRs62WLKYovDA8qMvybLTw8yIUoihp7I00ctJGJHDoEbhbIi0XHp9Ujdq0bNPlG-L5SoE9IFSoxX3ZQOZwSf90b_nDapbHJ8KCHZUnCBOwVnYiTXtpIQfrDVqqENarrIGa_uUbiriomYiB8gVkKWe6PB-I4lsYPEmMNnnpdvIf1eV_CsTmvUA54Ch1Zdip9mxrzRqrUqsx6vUTo0riCmiCxRg7mH2DuMaEPTZuQAMwhrQM_EwNsgx1yX1VsCKkL1Gu7CV_dqW5xxYlE7NEQmorT8W6aySbiBzsUWisJNnaR8RqZzeAUlSVMKBiw
{"odata.error":{"code":"Request_BadRequest","message":{"lang":"en","value":"Invalid domain name in the request url."},"requestId":"01ab745b-8a3f-48cc-9542-0c6abcae8950","date":"2020-02-17T22:41:28"}}
r

另外,请帮助我获取租户名称(是名称还是字母数字 ID?),目前我仅使用以下租户名称。 私有(private)常量字符串租户=“company.onmicrosoft.com”;

我只想在此代码中使用 Microsoft Graph API。谢谢。

最佳答案

i also want to know the whether this code is using Microsoft Graph or Azure AD.

此代码使用 Azure AD Graph API。如果请求 uri 正确,它就可以正常工作。您应该使用您的租户名称,而不是 company.onmicrosoft.com,并且必须指定 api 版本。所以请求uri应该是

var uri = "https://graph.windows.net/{your_tenant_name}.onmicrosoft.com/users?api-version=1.6"; 

如果租户名称错误,您将遇到无效域名错误。

I am not sure whether to use https://graph.microsoft.com or https://graph.windows.net.

您可以使用其中任何一个。但官方文档强烈建议您使用 Microsoft Graph 而不是 Azure AD Graph API 来访问 Azure Active Directory (Azure AD) 资源

如果您想使用 Microsoft Graph API,只需更改资源和请求 api url 的值即可。该资源将为 https://graph.microsoft.com。请求 api url 将为 var uri = "https://graph.microsoft.com/v1.0/users";

注意:请记住在 Azure 门户中向您的应用程序授予正确的权限并授予管理员同意。

enter image description here

关于c# - 域名无效 Azure Active Directory 问题和迁移到 Microsoft Graph C# 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60271462/

相关文章:

c# - 参数化查询 ..... 需要参数 '@units' ,但未提供

c# - 当我将文本框的此属性初始化为 false 时,属性 setenabled 保持 false

asp.net-mvc-3 - 是否可以加密免费网站模型上托管的 Azure 网站上的 Web.Config 连接字符串

azure - 将不属于目录中已验证域名的用户(成员)添加到 Azure Active Directory

Azure 自动化无法将文件传输到存储帐户

c# - 无法加载 Microsoft.Data.Services.Client 异常

c# - 在 c#.net 中将 MySQL 数据库导出到 EXCEL

c# - native C# : Reconstructing a Decimal number from its bit representation

azure - 使用 Azure REST API 指定 SAS token 授权 header

通过资源管理器(或代码)进行 Azure Active Directory B2C 部署