【问题标题】:How can I get a token from ADAL in Xamarin.iOS?如何从 Xamarin.iOS 中的 ADAL 获取令牌?
【发布时间】:2017-07-12 09:36:44
【问题描述】:

我想从 ADAL 获取令牌来验证特定的服务器调用。

我已尝试使用此代码:

var authorityUrl = string.Format(@"https://login.microsoftonline.com/{0}/oauth2/token", AadInstance);
var context = new AuthenticationContext(authorityUrl);
var credential = new ClientCredential(ClientId, ClientSecret);
var authenticationResult = context.AcquireTokenAsync(RemoteClientId, credential).Result;
return authenticationResult.AccessToken;

但我在日志中得到了这个:

AcquireTokenHandlerBase.cs: === Token Acquisition started:
    Authority: https://login.microsoftonline.com/f9e55202-63c0-4821-9fc7-e38eb5bc3a08/oauth2/token/
    Resource: 80d147c1-0b9a-48e0-8a62-1dc82890e98e
    ClientId: cab18d6f-3edc-446b-a071-45b28b192f0b
    CacheType: null
    Authentication Target: Client

TokenCache.cs: Looking up cache for a token...
TokenCache.cs: No matching token was found in the cache
AcquireTokenHandlerBase.cs: System.NullReferenceException: Object reference not set to an instance of an object
  at Microsoft.IdentityModel.Clients.ActiveDirectory.BrokerHelper.get_CanInvokeBroker () [0x0000c] in <f671779d8b3b49399b31bf519785e86e>:0 
  at Microsoft.IdentityModel.Clients.ActiveDirectory.AcquireTokenHandlerBase+<RunAsync>d__55.MoveNext () [0x00389] in <e4081d9da4634689910019c82c03f3e7>:0

我不知道这可能有什么问题,因为相同的代码在 Android 应用上按预期工作,而在 iOS 版本上却不工作。

【问题讨论】:

  • 您使用的是最新版本的 ADAL 吗?
  • @FeiXue-MSFT 是的,我是,使用版本 3.13.8.999

标签: ios xamarin.ios adal


【解决方案1】:

您的代码中有一些有趣的地方。首先,iOS 和 Android 应用程序是公共客户端,因此无法正确保护客户端机密。您永远不应该在您的应用程序中存储客户端密码。因此,客户端凭据流并不意味着或可能用于此场景,而是服务器到服务器应用程序身份验证。这可能是您错误的根本原因。

Here's a great sample 了解如何使用 ADAL 为所有 Android、iOS、Win Desktop、Windows Universal 构建 Xamarin 应用程序。我强烈建议遵循此处列出的模式。

【讨论】:

  • 那是我使用的库。我对 Active Directory 的了解不够,无法正确建议可以/应该做什么来验证呼叫。另外,服务器端不是我们自己实现的,所以我们不能轻易改变它。我最终手动实现了调用。
【解决方案2】:

我最终自己实现了调用:

public async Task<string> GetADALToken(string aadInstance, string clientId, string clientSecret, string remoteClientId)
{
    string body = $"resource={remoteClientId}&client_id={clientId}&client_secret={clientSecret}&grant_type=client_credentials";
    HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, $"https://login.microsoftonline.com/{aadInstance}/oauth2/token");

    byte[] byteArray = Encoding.UTF8.GetBytes(body);
    var content = new ByteArrayContent(byteArray);
    // set content type
    content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
    message.Content = content;

    message.Headers.Add("Accept", "application/json");

    HttpResponseMessage result = null;
    try
    {
        result = await _adalClient.SendAsync(message);
        result.EnsureSuccessStatusCode();
        var v = await result.Content.ReadAsStringAsync();
        return v;
    }
    catch (HttpRequestException reqExecption)
    {
        Log(reqExecption);
        if (result != null)
        {
            return "error " + await result.Content.ReadAsStringAsync();
        }
        return "error " + reqExecption.Message;
    }
    catch (Exception ex)
    {
        Log(ex);
        return "error " + ex.Message;
    }
}

【讨论】:

    猜你喜欢
    • 2016-01-04
    • 2019-05-15
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 2017-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多