2017-03-16 2 views
0

Azure AD 시스템에서 인증 토큰을 검색하려고합니다. await 명령과 함께 async 메서드를 구성하는 여러 가지 방법을 시도했지만 "작업이 취소되었습니다."라는 오류가 발생할 때마다 시도했습니다.웹 응용 프로그램의 Azure 비동기 인증 토큰

aspx 페이지에 async = "true"가 있습니다.

성공적인 요청을 받고 토큰을 검색하기 위해 다르게해야 할 아이디어가 있습니다.

동일한 코드가 콘솔 응용 프로그램에서 작동하므로 문제는 비동기 작업이 발생하는 방식과 관련이 있다고 가정합니다. 다음과 같이

내 코드는 다음과 같습니다

protected async void Login_click(object sender, EventArgs e) 
{ 
    response1.Text = "Started"; 
    var tentantID = ConfigurationManager.AppSettings["tenantID"]; 
    var clientId = ConfigurationManager.AppSettings["applicationID"]; 
    var secret = ConfigurationManager.AppSettings["secret"]; 

    await Authorize(tentantID , clientId, secret); 
} 
private async Task<AuthenticationResult> GetToken(string clientId, string tenantDomain, string clientSecret) 
{ 
    AuthenticationResult result = null; 

    var context = new AuthenticationContext("https://login.microsoftonline.com/" + tenantDomain); 

    try 
    { 
     ClientCredential clientCredential = new ClientCredential(clientId, clientSecret); 
     return result = await context.AcquireTokenAsync("https://management.core.windows.net/", clientCredential).ConfigureAwait(false); 

    } 
    catch (AdalException ae) 
    { 
     //Error code 
     return null; 
    } 
    catch (Exception e) 
    { 
     //Error code 
     return null; 
    } 
} 

private async Task Authorize(string tenant, string clientId, string clientSecret) 
{ 
    var authenticationResult = await GetToken(clientId, tenant, clientSecret).ConfigureAwait(false); 

    string token = authenticationResult.AccessToken; 
} 

편집이 ... 내 업데이트 된 코드 : ASP.NET에서

protected void Login_click(object sender, EventArgs e) 
{ 
    response1.Text = "Started"; 

    RegisterAsyncTask(new PageAsyncTask(Authorize)); 
} 
public async Task Authorize() 
{ 

    var tentantID = ConfigurationManager.AppSettings["tenantID"]; 
    var clientId = ConfigurationManager.AppSettings["applicationID"]; 
    string myKey = ConfigurationManager.AppSettings["myKey"]; 
    var tenantDomain = ConfigurationManager.AppSettings["tenantDomain"]; 

    var context = new AuthenticationContext("https://login.microsoftonline.com/" + tenantDomain, false); 

    try 
    { 
     ClientCredential clientCredential = new ClientCredential(clientId, myKey); 
     var result = context.AcquireTokenAsync("https://management.core.windows.net/", clientCredential).ConfigureAwait(false); 

     AuthenticationResult resVal = await result; 
     token = resVal.AccessToken; 
    } 
    catch (AdalException ae) 
    { 
     //error code 
     token = ae.InnerException.Message; 
    } 
} 
+0

다음 중 어떤 줄이 오류입니까? 'GetToken()'내부에서 디버깅 할 경우 * catch * 블록을 입력합니까? – Alisson

+0

AcquireTokenAsync 명령은 "A Task was cancelled"라는 내부 메시지가있는 AdalException을 throw합니다. –

답변

0

설명에 따르면 .NET Framework 4.5에서 웹 폼 응용 프로그램 대상을 만들고 Active Directory Authentication Library 3.13.8을 참조하여이 문제를 테스트했습니다. 귀하의 코드를 기반으로, 그것은 내 편이 예상대로 작동하고 Azure Web App에 배포 될 수 있습니다. 여기 내 git sample입니다, 당신은 그것을 참조하고 예상대로 작동 할 수 있는지 알아보십시오.

+0

고마워요. Azure Web App에 배포 한 후에는 작동하기 시작했으나 동일한 코드가 로컬에서 토큰을 제공하지 않습니다. –

0

async void 방법은 좋은 일이 아니다. 그것에 대한 정보는 https://www.hanselman.com/blog/TheMagicOfUsingAsynchronousMethodsInASPNET45PlusAnImportantGotcha.aspx을 참조하십시오.

나는 당신의 Login_Click 방법은 비동기 Authorize를 호출이 줄을 사용한다고 생각 :

RegisterAsyncTask(new PageAsyncTask(Authorize(tentantID, clientId, secret))); 

그리고 Login_Click 그냥 async 키워드없이 void를 반환해야합니다.

+0

슬프게도 나는 더 이상 없습니다. 제안 된대로 코드를 변경했지만, 더 이상이 사실을 알 수 없습니다. 나는 여전히 "작업이 취소되었습니다"라고 언급하기 전과 같은 메시지를받습니다. 편집 된 질문의 업데이트 된 코드. –

관련 문제