tutorial notificationhubs notification microsoft hub c# azure azure-api-apps azure-management-api

notificationhubs - face api c#



Autenticación de API Azure (1)

Estoy usando Azure API''s en el código C # y usado debajo de las bibliotecas

using Microsoft.Rest; using Microsoft.Rest.Azure.Authentication; using Microsoft.Azure.Management.DataLake.Store; using Microsoft.Azure.Management.DataLake.StoreUploader; using Microsoft.Azure.Management.DataLake.Analytics; using Microsoft.Azure.Management.DataLake.Analytics.Models; using Microsoft.WindowsAzure.Storage.Blob;

Para crear una conexión con Azure,

private static ServiceClientCredentials AuthenticateAzure(string domainName, string nativeClientAppCLIENTID) { // User login via interactive popup SynchronizationContext.SetSynchronizationContext(new SynchronizationContext()); // Use the client ID of an existing AAD "Native Client" application. var activeDirectoryClientSettings = ActiveDirectoryClientSettings.UsePromptOnly(nativeClientAppCLIENTID, new Uri("urn:ietf:wg:oauth:2.0:oob")); return UserTokenProvider.LoginWithPromptAsync(domainName, activeDirectoryClientSettings).Result; }

A través de esto obtuve la ventana emergente, que me pide mis credenciales. No quiero que aparezca esta ventana emergente todas las veces. ¿Hay alguna manera de pensar en esto además de crear la aplicación Azure?

======================================

Tengo ID de aplicación, TenantId y otra cosa. ¿Esto me ayudará a autenticarse en azul sin preguntar?


Podemos usar la función UserTokenProvider.LoginSilentAsync(nativeClientAppClientid, domainName, userName, password) para obtener nuestras credenciales sin pop-up. Funciona bien para mí, el siguiente es mi código de prueba. Cómo registrar WebApp, consulte el documento .

static void Main(string[] args) { var certificate = AuthenticateAzure("your domain name", "Ad App client ID", "username", "password"); } /// <summary> /// Log in to azure active directory in non-interactive mode using organizational // id credentials and the default token cache. Default service settings (authority, // audience) for logging in to azure resource manager are used. /// </summary> /// <param name="domainName"> The active directory domain or tenant id to authenticate with</param> /// <param name="nativeClientAppClientid"> The active directory client id for this application </param> /// <param name="userName"> The organizational account user name, given in the form of a user principal name (e.g. [email protected]).</param> /// <param name="password"> The organizational account password.</param> /// <returns>A ServiceClientCredentials object that can be used to authenticate http requests using the given credentials.</returns> private static ServiceClientCredentials AuthenticateAzure(string domainName, string nativeClientAppClientid,string userName,string password) { return UserTokenProvider.LoginSilentAsync(nativeClientAppClientid, domainName, userName, password).Result; }

Actualizar:

Más detalles acerca de cómo registrar la aplicación AD y asignar rol a la aplicación, consulte el documento . Después de eso, podemos obtener tenantId, appId, secretKey del Portal de Azure. Entonces podemos usar Microsoft.IdentityModel.Clients.ActiveDirectory SDK para obtener el token para la autenticación de API.

Código de demostración:

var subscriptionId = "Your subscrption"; var appId = "Registried Azure Application Id"; var secretKey = "Secret Key"; var tenantId = "tenant Id"; var context = new AuthenticationContext("https://login.windows.net/" + tenantId); ClientCredential clientCredential = new ClientCredential(appId, secretKey ); var tokenResponse = context.AcquireTokenAsync("https://management.azure.com/", clientCredential).Result; var accessToken = tokenResponse.AccessToken; using (var client = new HttpClient()) { client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken); client.BaseAddress = new Uri("https://management.azure.com/"); // Now we can party with our HttpClient! }