Je suis nouveau sur Azure et j’aimerais pouvoir, par programme, obtenir un jeton d’Azure. Quoi que je fasse cependant, cela semble échouer – quelqu’un a-t-il un exemple concret? Merci les gars
J’appelle GetAToken().Wait();
.
et la méthode est:
public async Task GetAToken() { // authentication parameters ssortingng clientID = "*********"; ssortingng username = ""; ssortingng password = ""; ssortingng directoryName = ""; ClientCredential cc = new ClientCredential(clientID, password); var authenticationContext = new AuthenticationContext("https://login.windows.net/" + directoryName); AuthenticationResult result = await authenticationContext.AcquireTokenAsync("https://management.core.windows.net/", cc); if (result == null) { throw new InvalidOperationException("Failed to obtain the JWT token"); } ssortingng token = result.AccessToken; return token; }
Donc, ne savez pas si vous le faites sur Android, iOS ou Xamarin.Forms. Voici comment je vais m’authentifier avec ADAL et Azure (le code fonctionne de mon côté):
Sur Android:
public async Task Authenticate(Activity context, ssortingng authority, ssortingng resource, ssortingng clientId, ssortingng returnUri) { var authContext = new AuthenticationContext(authority); if (authContext.TokenCache.ReadItems().Any()) authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority); var uri = new Uri(returnUri); var platformParams = new PlatformParameters(context); try { var authResult = await authContext.AcquireTokenAsync(resource, clientId, uri, platformParams); return authResult; } catch (AdalException e) { return null; } }
Sur iOS:
public async Task Authenticate(UIViewController controller, ssortingng authority, ssortingng resource, ssortingng clientId, ssortingng returnUri) { var authContext = new AuthenticationContext(authority); if (authContext.TokenCache.ReadItems().Any()) authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority); var controller = UIApplication.SharedApplication.KeyWindow.RootViewController; var uri = new Uri(returnUri); var platformParams = new PlatformParameters(controller); try { var authResult = await authContext.AcquireTokenAsync(resource, clientId, uri, platformParams); return authResult; } catch (AdalException e) { return null; } }
Sur UWP :
public async Task Authenticate(ssortingng authority, ssortingng resource, ssortingng clientId, ssortingng returnUri) { var authContext = new AuthenticationContext(authority); if (authContext.TokenCache.ReadItems().Any()) authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority); var uri = new Uri(returnUri); var platformParams = new PlatformParameters(PromptBehavior.Auto); try { var authResult = await authContext.AcquireTokenAsync(resource, clientId, uri, platformParams); return authResult; } catch (AdalException e) { return null; } }
Variable que je passe dans les méthodes ci-dessus:
ssortingng authority = "https://login.windows.net/common"; ssortingng ResourceID = "Backend ClientId";//Backend (web app) ssortingng clientId = "Native App ClientId";//native app ssortingng returnUri = "https://{My Azure Site}.azureewebsites.net/.auth/login/done";
Si vous voulez faire cela dans Xamarin.Forms, vous trouverez ci-dessous des liens vers ma solution GitHub où j’ai exposé ces méthodes via DependencyService
.
J’espère que ça aide! Si vous obtenez des erreurs dans votre réponse, vérifiez que vos permissions sont correctement configurées dans Azure. Je le fais comme ça . Le livre Xamarin / Azure d’Adrian Hall est une autre ressource précieuse.
Si vous essayez d’appeler les API Azure comme vous le souhaitez, vous devez procéder différemment.
Au final, vous obtiendrez toujours un jeton d’access que vous pourrez utiliser pour appeler l’API.