2017-02-22 16 views
6

我想从ADAL获取一个令牌来验证特定的服务器调用。如何在Xamarin.iOS中从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版本上不起作用。

+0

您使用的是最新版本的ADAL? –

+0

@ FeiXue-MSFT是的,我正在使用版本3.13.8.999 – vrwim

回答

0

你的代码中有一些有趣的事情。首先,iOS和Android应用程序是公共客户端,因此无法正确保护客户端机密。您绝不应该在您的应用中存储客户端机密。因此,客户端凭据流对于这种情况并不意味着或不可能,而是服务器到服务器应用程序认证。这可能是您错误的根源。

Here's a great sample如何使用ADAL为所有Android,iOS,Win Desktop,Windows Universal构建Xamarin应用程序。我强烈建议遵循这里提出的模式。

+0

这是我使用的库。我不太了解活动目录以正确地建议可以/应该做什么来验证呼叫。另外,服务器端不是由我们实施的,所以我们不能轻易改变它。我最终手动实现了通话。 – vrwim

-1

我最终实现自称:

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; 
    } 
}