缺少auth0的userInfo中的user_metadata

时间:2017-05-02 08:56:52

标签: authentication model-view-controller oauth-2.0 auth0

对于身份验证,我使用的是Auth0 AuthenticationApi。在Account Controller中,我需要获取user_metadata但它已丢失。获取user_metadata的任何替代方法?

AuthenticationApiClient client = new AuthenticationApiClient(new Uri($"https://{_auth0Options.Domain}/"));

               var authenticateResponse = await client.GetTokenAsync(new ResourceOwnerTokenRequest
                {
                    ClientId = _auth0Options.ClientId,
                    ClientSecret = _auth0Options.ClientSecret,
                    Scope = "openid",
                    Realm = _auth0Options.Connection,
                    Username = vm.EmailAddress,
                    Password = vm.Password
                });
                var user = await client.GetUserInfoAsync(authenticateResponse.AccessToken);
                if (user.UserMetadata != null)
                {
        // Giving error...any alternative to access the userMetaData ?
                }

1 个答案:

答案 0 :(得分:0)

是的,就我现在所看到的而言,传统呼叫仍然有效。但是,我还没有非传统解决方案:(

  using (var client = GetClient())
        {
            var jObject = new JObject(new JProperty("id_token", id_token));


            var response = await client.PostAsJsonAsync("tokeninfo", jObject);

            if (response.IsSuccessStatusCode)
            {
                var userProfileJson = JObject.Parse(await response.Content.ReadAsStringAsync());
                retVal.user_id = userProfileJson.Value<string>("user_id");
                retVal.email = userProfileJson.Value<string>("email");
                retVal.user_name = userProfileJson.Value<string>("nickname");
                if (userProfileJson.Value<string>("created_at") != null)
                {
                    retVal.created_at = userProfileJson.Value<DateTime>("created_at");
                }
                var exists = userProfileJson.TryGetValue("user_metadata", out JToken meta);