【问题标题】:how to properly implement Microsoft.IdentityModel.Clients.ActiveDirectory in xamarin 4如何在 xamarin 4 中正确实现 Microsoft.IdentityModel.Clients.ActiveDirectory
【发布时间】:2019-06-21 22:43:15
【问题描述】:

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

我尝试通过

获取令牌
  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);
                    }

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

无法从“Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior”转换为“Android.App.Activity”

无法从“Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior”转换为“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. 视觉工作室 2017

【问题讨论】:

  • 嘿,Akoh,您能否与我们分享更多代码。你把这段代码放在什么文件里?也许是您的 Git 存储库的链接?
  • @Saamer 感谢您与我们联系。我已经创建了一个要点,请看一下。

标签: xamarin.forms xamarin.android xamarin.ios azure-active-directory adal


【解决方案1】:

在您的共享项目中,使用 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);
     }

【讨论】:

    【解决方案2】:

    您需要将其转换为Activity

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

    【讨论】:

    • 谢谢,但这对我不起作用,因为它在共享代码库中
    • 平台参数需要在特定平台代码中单独实现,需要在iOS和Android上定义iplatformparameters并使用依赖服务
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    • 1970-01-01
    • 2020-08-07
    • 2020-10-11
    • 2021-11-03
    • 2021-12-13
    相关资源
    最近更新 更多