みなさん、こんにちは私の名前は谷口です
Azure Active Directoryへの認証を試みていますが、コード構文にエラーが表示されています。
インターフェースの認証:
public interface IAuthenticator
{
Task<AuthenticationResult> Authenticate(string authority, string resource, string clientId, string returnUri);
}
オーセンティケータークラス:
[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 = "[email protected]";
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タイプまたは名前空間名 'DependencyAttribute'が名前空間 'Xamarin.Android'に存在しません(アセンブリ参照がありませんか?)
[アセンブリ:Xamarin.Android.Dependency(typeof(App11.Authenticator))]
DependencyServiceは通常Xamarin.Formsで使用されますが、現在はXamarin.Androidプロジェクトです。IAuthenticatorクラスとAuthenticatorクラスは必要ありません。アクティビティに次のように直接メソッドを記述できます(Authenticateメソッドが機能するかどうかはわかりません。依存関係のエラー):
private async void Authenticate()
{
string Nome_Usuario = "[email protected]";
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;
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加