[英]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”(您是否缺少程序集引用?)
[组件: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依赖错误):
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.