2016-12-21 8 views
0

나는 내 자격 증명을 묻는 팝업을 가지고,이를 통해푸른 API 인증

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; 
    } 

를 C# 코드에서 푸른 API의의를 사용하여 라이브러리

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; 

푸른와 연결을 만들려면 아래 사용하고 있습니다. 나는이 팝업이 매번 나타나기를 원하지 않는다. Azure 앱을 만드는 것 외에이 문제를 해결할 방법이 있습니까?

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

나는 응용 프로그램 ID를 가지고 , TenantId와 다른 것. 이것이 내가 프롬프트없이 푸른 하늘을 인증하는 데 도움이됩니까?

enter image description here

+0

지침 ServicePrinicpal이 작업을 수행하기 위해 작성하는 방법을 여기에 있습니다 http://blog.davidebbo.com/2015/12/calling-arm-using -plain-rest.html –

답변

3

우리는 팝업없이 우리의 자격 증명을 얻을 수있는 기능 UserTokenProvider.LoginSilentAsync(nativeClientAppClientid, domainName, userName, password)을 사용할 수 있습니다. 그것은 나를 위해 잘 작동합니다, 다음은 내 테스트 코드입니다. 웹 애플리케이션 등록 방법은 document을 참조하십시오.

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; 
    } 

enter image description here

업데이트 : 응용 프로그램 역할을 AD 응용 프로그램 레지스트리 및 할당하는 방법에 대한

추가 사항 단계, document를 참조하십시오. 그 후 Azure Portal에서 tenantId, appId, secretKey을 얻을 수 있습니다. 그런 다음 Microsoft.IdentityModel.Clients.ActiveDirectory SDK를 사용하여 API 인증을위한 토큰을 얻을 수 있습니다.

데모 코드 :

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! 
} 

enter image description here

+0

ADLS 파일 액세스 및 일부 파일 조작에 Azure API를 사용하고 있으며 U-SQL 프로젝트와 통합하려고합니다. 이 경우 "도메인 이름"은 무엇입니까? 둘째. Azure 포털에 앱을 등록하고 싶지 않습니다. – Ajay

+0

Azure는 또한 자체 서명 된 관리 인증서 또는 Azure AD를 사용하여 인증을 허용합니다. Windows Azure 관리 인증서에 대한 자세한 내용은 [문서] (https://www.simple-talk.com/cloud/security-and-compliance/windows-azure-management-certificates/)를 참조하십시오. domain name :'기본적으로 .onmicrosoft.com의 기본 도메인 이름이 디렉토리에 포함됩니다. 나중에 조직에서 이미 사용하고있는 도메인 이름 (예 : contoso.com)을 추가 할 수 있습니다. ' –

+0

응용 프로그램 ID, TenantId 및 기타 항목이 있습니다. 이 메시지는 프롬프트없이 하늘을 인증하는 데 도움이됩니까? 내 질문을 편집하고 하나의 이미지를 첨부했습니다 ..이 줄에서 나를 도울 수 있습니까 .. 감사합니다 ... – Ajay