【问题标题】:Xamarin Android: How to authenticate to dynamicsXamarin Android:如何对动态进行身份验证
【发布时间】:2019-09-17 19:56:26
【问题描述】:

我找到了关于如何对动态进行身份验证的文档,但它是针对 xamarin 表单的,并且已经对其进行了测试。

但现在我想在不使用 xamarin 表单库的情况下在 xamarin Android 上正确地对动态进行身份验证

有没有一种方法可以对为 xamarin Android 制作的动态进行身份验证,而我不需要使用 xamarin 表单方式进行身份验证?

有关如何使用 xamarin 表单对动态进行身份验证的文档 https://www.c-sharpcorner.com/article/azure-active-directory-login-in-xamarin-forms/

我的身份验证器类:

[assembly: Xamarin.Forms.Dependency(typeof(Apontamento_Despesa.Authenticator))]

public class Authenticator : IAuthenticator
{
    public async Task<AuthenticationResult> Authenticate(string authority, string resource, string clientId, string returnUri)
    {
        var authContext = new AuthenticationContext(authority);
        if (authContext.TokenCache.ReadItems().Count() > 0)
            authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);
        var uri = new Uri(returnUri);

        var platformParams = new PlatformParameters((Activity)Forms.Context);
        var authResult = await authContext.AcquireTokenAsync(resource, clientId, uri, platformParams);
        return authResult;
    }
}

【问题讨论】:

    标签: c# xamarin.android microsoft-dynamics


    【解决方案1】:

    嗯,这很简单,实际上你所要做的就是从上述方法中删除所有与Xamarin.Forms 相关的代码,它应该对你有用,这样的东西应该可以工作

    public async Task<AuthenticationResult> Authenticate(string authority, string resource, string clientId, string returnUri, Activity activity)
    {
        var authContext = new AuthenticationContext(authority);
        if (authContext.TokenCache.ReadItems().Count() > 0)
            authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);
        var uri = new Uri(returnUri);
    
        var platformParams = new PlatformParameters(activity);
        var authResult = await authContext.AcquireTokenAsync(resource, clientId, uri, platformParams);
        return authResult;
    }
    

    其中 Activity 是调用活动。

    【讨论】:

    • 我遇到了异常,执行处于中断模式
    • 那个例外说明了什么?
    • 公共类认证器:IAuthenticator
    • 错误是:'Authenticator does not implement interface menbe' IAuthenticator.Authenticate(string,string,string,string)'
    • 我能够解决这个问题,但现在应用程序处于中断模式,因为活动有一个空值。
    【解决方案2】:
    [assembly: Xamarin.forms.Dependency(typeof(App11.Authenticator))]
    

    DependencyService 通常用在 Xamarin.Forms 中,但现在你是 Xamarin.Android 项目。你不需要 IAuthenticator 和 Authenticator 类,你可以直接在你的活动中编写方法(我不确定你的 Authenticate 方法是否有效,仅针对Dependency的错误):

    private  async void Authenticate()
    {
        string Nome_Usuario = "taniguchi.sales@ax4b.com";
        string clientId = "2b121ed5-9fe6-4ddf-bdea-9bbe8cd37bd0";
        string authority = "https://login.microsoftonline.com/ax4b.com";
        string returnUri = "https://ax4bdev.crm2.dynamics.com";
        string graphResourceUri = "https://ax4bdev.crm2.dynamics.com";
        var data = await Authenticate(authority, graphResourceUri, clientId, returnUri);
    
    }
    
    private async Task<AuthenticationResult> Authenticate(string authority, string resource, string clientId, string returnUri)
    {
        var authContext = new AuthenticationContext(authority);
        if (authContext.TokenCache.ReadItems().Count() > 0)
            authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);
        var uri = new Uri(returnUri);
    
        var platformParams = new PlatformParameters(this);
        var authResult = await authContext.AcquireTokenAsync(resource, clientId, uri, platformParams);
        return authResult;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-20
      • 1970-01-01
      • 2017-03-11
      • 2018-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多