如何在 xamarin 4 中正确实现 Microsoft.IdentityModel.Clients.ActiveDirectory

how to properly implement Microsoft.IdentityModel.Clients.ActiveDirectory in xamarin 4

安装 nuget 包后 Microsoft.IdentityModel.Clients.ActiveDirectory

I try to acquire token via

  string cloud = "https://login.microsoftonline.com/common/oauth2";
                    string tenantId = App.tenantId;
                    string authority = $"{cloud}/{tenantId}";

                    //
                    string clientId = App.clientId;
                    Uri redirectUri = App.redirectUrl;


                    string resource = clientId;


                     AuthenticationResult authResult = null;
                     AuthenticationContext authContext = new AuthenticationContext(authority);

                    try
                    {

                    if (authContext.TokenCache.ReadItems().Count() > 0)
                        {
                            authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);
                            authResult = await authContext.AcquireTokenSilentAsync(resource, clientId);
                        }
                        else
                        {
                            authResult = await authContext.AcquireTokenAsync(resource, clientId, redirectUri, new PlatformParameters(PromptBehavior.Auto));
                        }


                    }
                    catch (AdalSilentTokenAcquisitionException ee)
                    {
                        authResult = await authContext.AcquireTokenAsync(resource,clientId,  redirectUri,null);
                    }

当我尝试构建时出现以下错误

cannot convert from 'Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior' to 'Android.App.Activity'

cannot convert from 'Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior' to 'UIKit.UIViewController'

导致此错误的行是触发错误的行

                                authResult = await authContext.AcquireTokenAsync(resource, clientId, redirectUri, new PlatformParameters(PromptBehavior.Auto));

我该如何解决这个问题?

  1. Xamarin 版本为 4.0.0.482894
  2. Microsoft.IdentityModel.Clients.ActiveDirectory 版本 5.1.0
  3. Visual studio 2017

您需要将其转换为 Activity

new PlatformParameters((Activity) PromptBehavior.Auto));

在您的共享项目中,使用 IPlatformParameter ,如下所示:

 public class AuthenticationManager
    {
        private static string _authority;
        private static string _resource;
        private static string _clientId;
        private static string _returnUri;
        private static IPlatformParameters _parameters;
        private string _accessToken;

        public static UserInfo UserInfo { get; private set; }

        public static void SetConfiguration(string authority, string resource, string clientId, string returnUri)
        {
            _authority = authority;
            _resource = resource;
            _clientId = clientId;
            _returnUri = returnUri;

            var authContext = new AuthenticationContext(_authority);
            authContext.TokenCache.Clear();
        }

    public static void SetParameters(IPlatformParameters parameters)
    {
        _parameters = parameters;
    }

        public async Task<bool> LoginAsync()
        {
            _accessToken = await GetAccessTokenAsync();
            return true;
        }

        public Task LogoutAsync()
        {
            var authContext = new AuthenticationContext(_authority);
            var cachedToken = authContext.TokenCache.ReadItems().FirstOrDefault(t => t.Authority == _authority && t.ClientId == _clientId && t.Resource == _resource);

            if (cachedToken != null)
            {
                authContext.TokenCache.DeleteItem(cachedToken);
            }

            UserInfo = null;
            _accessToken = null;

            return Task.CompletedTask;
        }



        private async Task<string> GetAccessTokenAsync()
        {
            var uri = new Uri(_returnUri);

            var authContext = new AuthenticationContext(_authority);
            var authResult = await authContext.AcquireTokenAsync(_resource, _clientId, uri, _parameters);

            UserInfo = authResult.UserInfo;

            return authResult.AccessToken;
        }
    }

然后在特定平台中,调用 SetParameters 方法: MainActivity.cs 上 Android 中的示例:

protected override void OnCreate(Bundle bundle)
     {
         var platformParameters = new PlatformParameters(this);
         AuthenticationManager.SetParameters(platformParameters);
     }

类似 iOS:

 public override void ViewDidLoad()
     {
         var platformParameters = new PlatformParameters(this);
         AuthenticationManager.SetParameters(platformParameters);
     }