我需要在用户认证后获取电子邮件地址。
我试图在authenticationResult中找到这些信息,但我只是找到了用户名..但不是电子邮件。
我如何获得这些信息?在xamarin应用中的Azure AD认证之后获取用户电子邮件
谢谢
我需要在用户认证后获取电子邮件地址。
我试图在authenticationResult中找到这些信息,但我只是找到了用户名..但不是电子邮件。
我如何获得这些信息?在xamarin应用中的Azure AD认证之后获取用户电子邮件
谢谢
你想在客户端或服务器端的电子邮件?如果它是服务器端,请尝试检查x-ms-client-principal-name HTTP标头值。如果是客户端,请尝试向/.auth/me发送经过验证的请求,并且您应该看到所有声明,包括JSON响应中的用户电子邮件。
既然您在另一个答案中提到这是客户端,请使用InvokeApi <>()方法。 https://adrianhall.github.io/develop-mobile-apps-with-csharp-and-azure/chapter2/authorization/#obtaining-user-claims
短的版本是这样的代码:
List<AppServiceIdentity> identities = null;
public async Task<AppServiceIdentity> GetIdentityAsync()
{
if (client.CurrentUser == null || client.CurrentUser?.MobileServiceAuthenticationToken == null)
{
throw new InvalidOperationException("Not Authenticated");
}
if (identities == null)
{
identities = await client.InvokeApiAsync<List<AppServiceIdentity>>("/.auth/me");
}
if (identities.Count > 0)
return identities[0];
return null;
}
凡AppServiceIdentity这样定义:
public class AppServiceIdentity
{
[JsonProperty(PropertyName = "id_token")]
public string IdToken { get; set; }
[JsonProperty(PropertyName = "provider_name")]
public string ProviderName { get; set; }
[JsonProperty(PropertyName = "user_id")]
public string UserId { get; set; }
[JsonProperty(PropertyName = "user_claims")]
public List<UserClaim> UserClaims { get; set; }
}
public class UserClaim
{
[JsonProperty(PropertyName = "typ")]
public string Type { get; set; }
[JsonProperty(PropertyName = "val")]
public string Value { get; set; }
}
我没有找到InvokeApiAsync来对此进行了详细书中这里讨论叫它。 是否有一个令牌或类似的东西来找到电子邮件?
var client = new HttpClient();
VAR请求=新HttpRequestMessage(HttpMethod.Get,CloudConstants.ApIbaseUrl + /.auth/me“);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _authenticationResult.Token);
try
{
var response = client.SendAsync(request);
if (response.Result.IsSuccessStatusCode)
{
var responseString = response.Result.Content.ReadAsStringAsync();
var profile = JArray.Parse(responseString.Result);
}
}
catch (Exception ee)
{
_dialogService.DisplayAlertAsync("An error has occurred", "Exception message: " + ee.Message, "Dismiss");
}
是它的HT客户端 – Stephane
我试试这个代码,但没有结果 – Stephane
VAR的客户端。 = new HttpClient(); var request = new HttpRequestMessage(HttpMethod.Get,CloudConstants.ApIbaseUrl +“/.auth/me”); try {var response = client.SendAsync(request); if(response.Result.IsSuccessStatusCode ) { var responseString = response.Result.Content.ReadAsStringAsync(); var profile = JArray.Pars E(responseString.Result); } } catch(Exception ee) – Stephane