简体   繁体   English

无效的域名 Azure 活动目录问题和迁移到 Microsoft Graph C# 应用程序

[英]Invalid domain name Azure Active directory issue and Migration to Microsoft Graph C# application

I am trying to run below code to get the user details as response from Azure Active Directory :我正在尝试运行以下代码以获取用户详细信息作为 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"; 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;



        }
    }
}

I am getting below error:我收到以下错误:

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

Also ,help me in getting tenant name ( it's a name or alphanumeric id ?) ,currently i am just using below tenant name.另外,请帮助我获取租户名称(它是名称还是字母数字 ID?),目前我只使用以下租户名称。 private const string tenant = "company.onmicrosoft.com"; private const string tenant = "company.onmicrosoft.com";

I only want to use Microsoft Graph API in this code.我只想在此代码中使用 Microsoft Graph API。 Thanks.谢谢。

i also want to know the whether this code is using Microsoft Graph or Azure AD.我还想知道此代码是使用 Microsoft Graph 还是 Azure AD。

This code is using Azure AD Graph API.此代码使用 Azure AD Graph API。 It works fine if the request uri is correct.You should use your tenant name, not company.onmicrosoft.com , and you must specify the api-version.如果请求 uri 正确,则它工作正常。您应该使用您的租户名称,而不是company.onmicrosoft.com ,并且您必须指定 api-version。 So the request uri should be所以请求uri应该是

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

If the tenant name is wrong, you will encounter Invalid domain name error.如果租户名称错误,则会遇到 Invalid domain name 错误。

I am not sure whether to use https://graph.microsoft.com or https://graph.windows.net .我不确定是使用https://graph.microsoft.com还是https://graph.windows.net

You can use either one.您可以使用任何一种。 But the official document strongly recommend that you use Microsoft Graph instead of Azure AD Graph API to access Azure Active Directory (Azure AD) resources .但是官方文档强烈建议您使用 Microsoft Graph 而不是 Azure AD Graph API 来访问 Azure Active Directory (Azure AD) 资源

If you want to use Microsoft Graph API, just change the value of resource and request api url.如果要使用Microsoft Graph API,只需更改resource 的值并请求api url。 The resource will be https://graph.microsoft.com .该资源将是https://graph.microsoft.com The request api url will be var uri = "https://graph.microsoft.com/v1.0/users";请求 api url 将是var uri = "https://graph.microsoft.com/v1.0/users"; . .

Note: Remember to grant your application the correct permissions in Azure portal and grant admin consent.注意:请记住在 Azure 门户中授予您的应用程序正确的权限并授予管理员同意。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM