.net核心2.1的Auth0实现

时间:2018-11-27 13:20:24

标签: auth0

无法使用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响应= client.Execute(request);

给予: {     “错误”:“ access_denied”,     “ error_description”:“在域https://satyamdev.auth0.com/api/v2/users/中未启用服务” }

1 个答案:

答案 0 :(得分:2)

我认为您在请求中提供了未定义的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获取请求,以获取整个用户个人资料。

卷曲命令:

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

在Dotnet core 2.1中,您可以尝试以下操作来获取令牌并使用令牌来获取用户:

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(读取:用户)。