private async Task <string> GetAppTokenAsync() { // *****ADAL code Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationResult tokenResult = null; string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value; Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext authContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(authority, false); Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential credential = new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential(clientId, appKey); tokenResult = await authContext.AcquireTokenAsync(resAzureGraphAPI, credential); return(tokenResult.AccessToken); }
private async Task RedeemUsernameAndPasswordAsync() { if (string.IsNullOrEmpty(this.AccessToken)) { var creds = new Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential(this.Username, this.Password); var context = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(this.TokenService); var token = await context.AcquireTokenAsync(this.Resource, this.ClientId, creds); if (null != token) { this.AccessToken = token.AccessToken; } else { throw new OAuthAccountException( string.Format("Failed to convert username + password to access token for account: {0}", this.Name)); } } }
/// <summary> /// get the bearer token for adding to authorization header /// </summary> /// <returns></returns> private async static Task <string> GetBearerToken() { try { string authContextURL = "https://login.windows.net/" + tenantId; var authenticationContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(authContextURL); var cred = new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential(clientId, clientKey); var result = await authenticationContext.AcquireTokenAsync("https://management.azure.com/", cred); if (result == null) { throw new InvalidOperationException("Failed to obtain the JWT token"); } string token = result.AccessToken; return(token); } catch (Exception ex) { throw ex; } }
private async Task RedeemUsernameAndPasswordAsync() { if (string.IsNullOrEmpty(this.AccessToken)) { var creds = new Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential(this.Username, this.Password); var context = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(this.TokenService); var token = await context.AcquireTokenAsync(this.Resource, this.ClientId, creds); if (null != token) { this.AccessToken = token.AccessToken; } else { throw new InvalidOperationException( string.Format("Failed to convert username + password to access token for account: {0}", this.Name)); } } }
private async Task <string> getTokenAsync() { var tenantName = ConfigurationManager.AppSettings.Get("tenantName"); var aad = ConfigurationManager.AppSettings.Get("aad"); var authority = string.Format(CultureInfo.InvariantCulture, aad, tenantName); var clientId = ConfigurationManager.AppSettings.Get("clientId"); var authContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(authority); X509Certificate2 cert = Helper.GetCertificateBySubject(); var certCred = new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientAssertionCertificate(clientId, cert); string token = null; Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationResult result = await authContext.AcquireTokenAsync(_resource, certCred); if (result != null) { token = result.AccessToken; } return(token); }
private async void UserAuthentication_Click(object sender, RoutedEventArgs e) { string msg = string.Empty; int count = -1; string Token = null; LogMessage("Interactive User Authentication - Azure Tenant: " + azureActiveDirectoryTenantDomain.Text); string authority = "https://login.microsoftonline.com/" + azureActiveDirectoryTenantDomain.Text; Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext ac = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(authority); string resourceUrl = "https://rest.media.azure.net"; string redirectUri = "https://AzureMediaServicesNativeSDK"; Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationResult result = null; try { Microsoft.IdentityModel.Clients.ActiveDirectory.IPlatformParameters param = new Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformParameters(Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior.Always, false);; result = await ac.AcquireTokenAsync(resourceUrl, "d476653d-842c-4f52-862d-397463ada5e7", new Uri(redirectUri), param); } catch (Exception ex) { LogMessage("Interactive User Authentication - Exception: " + ex.Message); } if (result != null) { Token = result.AccessToken; if (!string.IsNullOrEmpty(Token)) { LogMessage("Interactive User Authentication successful"); LogMessage("Token: " + Token); try { LogMessage("Getting Media Objects - Account Name: " + legacyAccountName.Text + " from region " + azureRegion.Text); count += await GetMediaObjectsCount <Asset>(Token, legacyAccountName.Text, azureRegion.Text); count += await GetMediaObjectsCount <MediaProcessor>(Token, legacyAccountName.Text, azureRegion.Text); count += await GetMediaObjectsCount <Channel>(Token, legacyAccountName.Text, azureRegion.Text); count += await GetMediaObjectsCount <AzureMediaServicesREST.Program>(Token, legacyAccountName.Text, azureRegion.Text); count += await GetMediaObjectsCount <StreamingEndpoint>(Token, legacyAccountName.Text, azureRegion.Text); count += await GetMediaObjectsCount <Locator>(Token, legacyAccountName.Text, azureRegion.Text); count += await GetMediaObjectsCount <AccessPolicie>(Token, legacyAccountName.Text, azureRegion.Text); SaveSettings(); } catch (Exception ex) { msg = ex.Message; LogMessage("Getting Media Objects - Exception: " + msg); } if (count >= 0) { Show("Interactive User Authentication successful: " + count.ToString() + " asset(s) found"); } else { Show("Interactive User Authentication failed" + (string.IsNullOrEmpty(msg) ? "" : ": Exception - " + msg)); } } else { Show("Interactive User Authentication failed"); } } else { Show("Interactive User Authentication failed"); } }
public async static System.Threading.Tasks.Task <string> GetToken(string authority, string resource, string scope) { var authContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(authority); Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationResult result = await authContext.AcquireTokenAsync(resource, _clientCredential); if (result == null) { throw new System.InvalidOperationException("Failed to obtain the access token"); } return(result.AccessToken); }
public async Task AuthenticateRequestAsync(HttpRequestMessage request) { var result = await _authenticationContext.AcquireTokenAsync("https://graph.microsoft.com", EutConfiguration.Current.ClientCredential); request.Headers.Add("Authorization", result.CreateAuthorizationHeader()); }
private static string GetBearerToken() { string bearerToken = null; string authority = "https://login.microsoftonline.com/common/oauth2/" + "78eff5bb-da38-47f0-a836-294c6d784112"; string clientId = "bca42905-7439-47c7-a349-ef064fa6e8d6"; string clientSecret = "AWq9PpVl-@r_TpHTIoigicsX4?s5yHa4"; string resource = "https://management.core.windows.net/"; Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext authContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(authority); Task <Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationResult> authResultTask = authContext.AcquireTokenAsync(resource, new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential(clientId, clientSecret)); bearerToken = authResultTask.Result.AccessToken; return(bearerToken); }