简体   繁体   English

Xamarin Android:为什么对 azure 活动目录的身份验证不起作用

[英]Xamarin Android: Why authentication to azure active diretory is not working

Hello everyone my name is Taniguchi大家好我叫谷口

I am trying to authenticate to azure active directory but the code sintaxe is showing an error.我正在尝试对 azure 活动目录进行身份验证,但代码 sintaxe 显示错误。

Authenticate interface:认证界面:

public interface IAuthenticator
{
    Task<AuthenticationResult> Authenticate(string authority, string resource, string clientId, string returnUri);
}

authenticator class:验证器 class:

[assembly: Xamarin.Android.Dependency(typeof(App11.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)Android.Context);
        var authResult = await authContext.AcquireTokenAsync(resource, clientId, uri, platformParams);
        return authResult;
    }
}

where i calling the authentification:我在哪里调用身份验证:

public class MainActivity : AppCompatActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.activity_main);
        Authenticate();
    }

    public 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 auth = Xamarin.Android.DependencyService.Get<IAuthenticator>();
        var data = await auth.Authenticate(authority, graphResourceUri, clientId, returnUri);
    }

    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
        AuthenticationAgentContinuationHelper.SetAuthenticationAgentContinuationEventArgs(requestCode, resultCode, data);
    }

The Line:线路:

[assembly: Xamarin.Android.Dependency(typeof(App11.Authenticator))]

is showing the error: Error CS0234 The type or namespace name 'DependencyAttribute' does not exist in the namespace 'Xamarin.Android' (are you missing an assembly reference?)显示错误:错误 CS0234 命名空间“Xamarin.Android”中不存在类型或命名空间名称“DependencyAttribute”(您是否缺少程序集引用?)

[assembly: Xamarin.Android.Dependency(typeof(App11.Authenticator))] [组件:Xamarin.Android.Dependency(typeof(App11.Authenticator))]

DependencyService is usually used in Xamarin.Forms ,but now you are Xamarin.Android project.you dont need IAuthenticator and Authenticator class,you could write the method in your activity directly like (I'm not sure your Authenticate method works,only for the error of Dependency): DependencyService is usually used in Xamarin.Forms ,but now you are Xamarin.Android project.you dont need IAuthenticator and Authenticator class,you could write the method in your activity directly like (I'm not sure your Authenticate method works,only for the依赖错误):

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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Xamarin Azure活动目录重定向URI错误 - Xamarin Azure Active Diretory Redirect URI Error azure 移动服务无法在 xamarin.android 上运行 - azure mobile service not working on xamarin.android 在 Azure Active Directory 身份验证中为什么使用授权代码流 - In Azure Active Directory authentication why is authorisation code flow used 使用 Azure Active Directory 的 Azure Function 身份验证 - Azure Function authentication using Azure Active Directory Xamarin Android和Azure SQL - Xamarin Android and Azure SQL 使用Azure Active Directory授权Xamarin PCL应用 - Authorizing Xamarin PCL app with Azure Active Directory 使用Xamarin Android进行Firebase Facebook身份验证 - Firebase Facebook Authentication with Xamarin Android 为什么Azure Active Directory身份验证会在ASP.NET Core应用程序中引起字符编码错误? - Why is Azure Active Directory Authentication causing an character encoding error in my ASP.NET Core Application? 使用 Angular 8 和 Azure Active Directory 进行身份验证 - Authentication using Angular 8 with Azure Active Directory 为什么TextChanged事件在此Xamarin.Android代码中不起作用? - Why is the TextChanged event not working in this Xamarin.Android code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM