我想从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 :(得分:0)
你的代码中有一些有趣的东西。首先,iOS和Android应用程序是公共客户端,因此无法正确保护客户端密钥。您永远不应该在您的应用中存储客户端密钥。因此,客户端凭据流对于此方案不是意味着或不可能,而是服务器到服务器应用程序身份验证。这可能是您的错误的根本原因。
Here's a great sample如何使用ADAL为所有Android,iOS,Win桌面,Windows Universal构建Xamarin应用程序。我强烈建议遵循这里提出的模式。
答案 1 :(得分:-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;
}
}