Auth0 implmentation with .net core 2.1

Auth0 implmentation with .net core 2.1

无法使用 C# 在 auth0 中创建令牌

var client = new RestClient("https://domain/oauth/token");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", "{\"client_id\":\"#####\",\"cli``ent_secret\":\"####\",\"audience\":\"https://domain/api/v2/users\",\"grant_type\":\"client_credentials\"}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

给予:

{
    "error": "access_denied",
    "error_description": "Service not enabled within domain: https://satyamdev.auth0.com/api/v2/users/"
}

我认为您在请求中提供了未定义的 API 标识符(受众)。受众参数应为 https://[domain].auth0.com/api/v2/

curl 命令示例:

正在执行客户端凭据授予类型以获取令牌。

curl --request POST \
  --url 'https://[Domain].auth0.com/oauth/token'  \
  --header 'content-type: application/json' \
  --data '{"grant_type":"client_credentials","client_id":"[Client ID]","client_secret": "[Client secret]","audience": "https://[Domain].auth0.com/api/v2/"}'

获得令牌后,您可以向端点 /api/v2/users/{id} 发出 HTTP get 请求以获取整个用户配置文件。

卷曲命令:

curl -X GET \
--url "https://[Domain].auth0.com/api/v2/users" \
-H "Content-Type:application/json" \
-H "Authorization:Bearer [Token]"

在Dotnet core 2.1中,您可以尝试以下方式获取token并使用token获取用户:

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace ClientCredentials {
    class Program {
        private static string accessToken;
        private static async Task Main (string[] args) {
            await ClientCredentialsFlow ();
            await GetUsers ();
            // await CreateUser();
        }

        protected static async Task ClientCredentialsFlow () {

            var body = new Model {
                grant_type = "client_credentials",
                client_id = "[client id]",
                client_secret = "[client secret]",
                audience = "https://[domain].auth0.com/api/v2/"
            };

            using (var client = new HttpClient ()) {
                var content = JsonConvert.SerializeObject (body);
                var stringContent = new StringContent (content, Encoding.UTF8, "application/json");
                var res = await client.PostAsync ("https://[domain].auth0.com/oauth/token", stringContent);
                var responseBody = await res.Content.ReadAsStringAsync ();
                var deserilizeBody = JsonConvert.DeserializeObject<AuthResponseModel> (responseBody);
                accessToken = deserilizeBody.access_token;
                Console.WriteLine (accessToken);

            }

        }
        protected static async Task GetUsers () {
            using (var client = new HttpClient ()) {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue ("Bearer", accessToken);
                var response = await client.GetAsync ("https://[domain].auth0.com/api/v2/users");
                var responseBody = await response.Content.ReadAsStringAsync ();
                Console.WriteLine ("==============================");
                Console.WriteLine (responseBody);

            }
        }

        internal class Model {

            public string grant_type { get; set; }
            public string client_id { get; set; }
            public string client_secret { get; set; }
            public string audience { get; set; }
        }

        internal class AuthResponseModel {
            public string access_token { get; set; }
            public string scopes { get; set; }
            public string expires_in { get; set; }
            public string token_type { get; set; }
        }

        internal class User {
            public string email { get; set; }
            public bool email_verified { get; set; }
            public string connection { get; set; }
            public string username { get; set; }
            public string password { get; set; }

        }

    }
}

注意:要调用 /api/v2/users 端点,您需要有正确的 permissions (read:users)。