使用针对Azure AD的ADAL对Xamarin应用进行身份验证

时间:2017-01-13 14:01:30

标签: c# azure xamarin.ios xamarin.forms adal

我有一个Xamarin.Forms可移植项目,现在仅适用于iOS,我们使用ADAL库对Azure AD进行身份验证。当用户输入用户名和密码时,它非常有效。

但现在我需要创建一个没有用户名/密码的应用程序版本。我使用App键创建了一个新的Azure AD应用程序,并使用此应用程序/密钥更改了应用程序以识别它。

在控制台应用程序中测试身份验证类时,我收到了Bearer令牌。但是在Xamarin项目中,我在调用AcquireTokenAsync时遇到了Null指针异常。

这里的任何一个设法让这个工作能够指出我正确的方向吗?

不同的id /键被假的替换......

ADAL 3.13.8版。

这是进行身份验证的类,在最后一个函数中抛出异常:

internal class AppAuthenticationHelper
{
    private const string AadInstance = "https://login.microsoftonline.com/";

    private const string Tenant = "xxx.yyyyy.com";
    private const string ClientId = "ddsgreg-grg-rgerg-argerg-qrgqreg";
    private const string AppIdUri = "https://xxx.yyyy.com";
    private const string AppKey = "asegfuoyagfoygaefouygauyfaeuyrfg2;

    private static readonly string Authority = $"{AadInstance}{Tenant}";

    private readonly AuthenticationContext authContext;
    private readonly ClientCredential clientCredential;

    public AppAuthenticationHelper()
    {
        authContext = new AuthenticationContext(Authority);
        clientCredential = new ClientCredential(ClientId, AppKey);
    }

    public async Task<AuthenticationResult> AcquireTokenAsync()
    {
        return await authContext.AcquireTokenAsync(AppIdUri, clientCredential);
    }
}

控制台测试程序:

    static void Main(string[] args)
    {
        var auth = new AppAuthenticationHelper();
        var t = auth.AcquireTokenAsync();
        t.Wait(TimeSpan.FromSeconds(30));
        if (t.IsCompleted)
        {
            Console.WriteLine(t.Result.AccessTokenType);
            Console.WriteLine(t.Result.AccessToken);
        }

        Console.ReadLine();
    }

显示Xamarin代码有点复杂,但这里有一些部分:

    private async Task<AuthenticationHeaderValue> CreateAuthHeader()
    {
        var appAuth = new AppAuthenticationHelper();
        var ar = await appAuth.AcquireTokenAsync();
        if (ar != null)
        {
            return new AuthenticationHeaderValue(ar.AccessTokenType, ar.AccessToken);
        }

        throw new Exception();
    }


    protected HttpClient GetHttpClient()
    {
        var httpClient = HttpMessageHandler != null ? new HttpClient(HttpMessageHandler) : new HttpClient();

        httpClient.BaseAddress = new Uri(BaseAddress);
        httpClient.DefaultRequestHeaders.Accept.Clear();
        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        return httpClient;
    }

    protected async Task<string> SendAsync(string requestUri, string payload)
    {
        var request = new HttpRequestMessage(HttpMethod.Post, requestUri);
        request.Headers.Authorization = AuthHeader;
        request.Content = new StringContent(payload, Encoding.UTF8, "application/json");

        using (var httpClient = GetHttpClient())
        {
            var httpResponse = await httpClient.SendAsync(request, CancellationToken);
            httpResponse.EnsureSuccessStatusCode();
            return await httpResponse.Content.ReadAsStringAsync();
        }
    }

0 个答案:

没有答案