Xamarin Android:为什么对 Azure 活动目录的身份验证不起作用
Xamarin Android: Why authentication to azure active diretory is not working
大家好我是谷口
我正在尝试对 Azure 活动目录进行身份验证,但代码 sintaxe 显示错误。
认证接口:
public interface IAuthenticator
{
Task<AuthenticationResult> Authenticate(string authority, string resource, string clientId, string returnUri);
}
验证者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;
}
}
我在哪里调用认证:
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);
}
线路:
[assembly: Xamarin.Android.Dependency(typeof(App11.Authenticator))]
显示错误:
错误 CS0234 命名空间 'Xamarin.Android' 中不存在类型或命名空间名称 'DependencyAttribute'(是否缺少程序集引用?)
[assembly: Xamarin.Android.Dependency(typeof(App11.Authenticator))]
DependencyService平时用在Xamarin.Forms,但是现在你是Xamarin.Android project.you 不需要 IAuthenticator 和 Authenticator class ,你可以把方法写在你的 activity直接点赞(我不确定你的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;
}
大家好我是谷口
我正在尝试对 Azure 活动目录进行身份验证,但代码 sintaxe 显示错误。
认证接口:
public interface IAuthenticator
{
Task<AuthenticationResult> Authenticate(string authority, string resource, string clientId, string returnUri);
}
验证者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;
}
}
我在哪里调用认证:
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);
}
线路:
[assembly: Xamarin.Android.Dependency(typeof(App11.Authenticator))]
显示错误: 错误 CS0234 命名空间 'Xamarin.Android' 中不存在类型或命名空间名称 'DependencyAttribute'(是否缺少程序集引用?)
[assembly: Xamarin.Android.Dependency(typeof(App11.Authenticator))]
DependencyService平时用在Xamarin.Forms,但是现在你是Xamarin.Android project.you 不需要 IAuthenticator 和 Authenticator class ,你可以把方法写在你的 activity直接点赞(我不确定你的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;
}