2017-01-20 4 views
0

같은 푸른빛 활성 디렉토리를 사용하는 두 개의 응용 프로그램이 있습니다. 응용 프로그램 A와 응용 프로그램 B.흐름 대신 Azure AD와 쿠키를 사용하여 다른 리소스에 대한 액세스 토큰을 얻는 방법

응용 프로그램 A는

app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions 
     { 

      AutomaticAuthenticate = true, 
      AutomaticChallenge = true, 
      ClientId = Configuration["Authentication:AzureAd:ClientId"], 
      Authority = Configuration["Authentication:AzureAd:AADInstance"] + Configuration["Authentication:AzureAd:TenantId"], 
      ClientSecret = Configuration["Authentication:AzureAd:ClientSecret"], 
      CallbackPath = Configuration["Authentication:AzureAd:CallbackPath"],      
      ResponseType = OpenIdConnectResponseType.CodeIdToken, 
      GetClaimsFromUserInfoEndpoint = true, 
      SignInScheme = "Cookies", 
      SaveTokens = true,                
      Events = new OpenIdConnectEvents 
      { 
       OnAuthorizationCodeReceived = OnAuthorizationCodeReceived, 
      }   

     }); 

를 사용하고 난으로 토큰을 획득하여 응용 프로그램 B API 서비스 리소스에 대한 액세스 권한을 획득 : 나는 또한 쿠키를 사용하고 있습니다

private async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedContext context) 
    {   
     string userObjectId = (context.Ticket.Principal.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier"))?.Value; 
     ClientCredential clientCred = new ClientCredential(Configuration["Authentication:AzureAd:ClientId"], Configuration["Authentication:AzureAd:ClientSecret"]); 
     AuthenticationContext authContext = new AuthenticationContext(Configuration["Authentication:AzureAd:AADInstance"] + Configuration["Authentication:AzureAd:TenantId"]); 
     AuthenticationResult authResult = await authContext.AcquireTokenByAuthorizationCodeAsync(
      context.ProtocolMessage.Code, new Uri(context.Properties.Items[OpenIdConnectDefaults.RedirectUriForCodePropertiesKey]), clientCred, Configuration["Authentication:AzureAd:GraphResourceId"]); 

앱 A에 로그인하려면 다음을 입력하십시오.

app.UseCookieAuthentication(new CookieAuthenticationOptions() 
     { 
      AuthenticationScheme = "Cookies", 
      AutomaticAuthenticate = true, 
      AutomaticChallenge = true, 
      SlidingExpiration = true, 
      ExpireTimeSpan = TimeSpan.FromHours(1), 
      Events = new CookieAuthenticationEvents() 
      { 
       OnSignedIn = OnSignedIn, 
       OnSigningIn = OnSigningIn, 
       OnValidatePrincipal = OnValidatePrincipal      
      } 
     }); 
/* Account Controller SignIn() */ 
return Challenge(
      new AuthenticationProperties { 
       AllowRefresh = true, 
       IsPersistent = true,          
       RedirectUri = "/" }, OpenIdConnectDefaults.AuthenticationScheme); 

이제 내 문제는 내 액세스 토큰 앱에 대한 내 로그인 쿠키는 여전히 유효하므로 캐시에 토큰이 없더라도 사용자는 정상적으로 로그인 한 것처럼 보입니다.

나는 다른 질문의 소송을 따라 여기에 내가 다시 기본에이 토큰을 얻을 ADAL AuthenticationContext가 사용하는 TokenStore하는 방법을 몰라

Task OnValidatePrincipal(CookieValidatePrincipalContext arg) { 

    var http = new HttpClient(); 
       var uri = "https://login.microsoftonline.com/<tenant>/oauth2/token"; 
       var client_id = "<my_client_id>"; 
       var scope = "https://graph.microsoft.com/mail.read"; 
       var refresh_token = "<saved_refresh_token_in_cookie_if_SaveTokens = true>"; 
       var redirect_uri = "https://localhost:20352/"; 
       var grant_type = "refresh_token"; 
       var client_secret = "<client_secret_from_azure>"; 
       var body = new List<KeyValuePair<string, string>> 
         { 
          new KeyValuePair<string, string>("client_id", client_id), 
          new KeyValuePair<string, string>("scope", scope), 
          new KeyValuePair<string, string>("refresh_token", refresh_token), 
          new KeyValuePair<string, string>("redirect_uri", redirect_uri), 
          new KeyValuePair<string, string>("grant_type", grant_type), 
          new KeyValuePair<string, string>("client_secret", client_secret) 
         }; 

       var content = new FormUrlEncodedContent(body); 

       var result = http.PostAsync(uri, content).Result; 
       var stringContent = result.Content.ReadAsStringAsync().Result; 

       JObject jobject = JObject.Parse(stringContent); 
       var token = jobject["access_token"].Value<string>(); 

문제의 내 쿠키 이벤트에보고했습니다. 대신에 '토큰 새로 고침/유효한 토큰없이,

_authenticationResult = await authContext.AcquireTokenSilentAsync(_authConfigOptions.AzureAd.WebserviceAppIdUri.ToString(), credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId)); 

내가 토큰을 다시 사용자 응용 프로그램 B API 호출에 대한 tokenstore에 새 자원 액세스를 얻을 수있는 방법이 있나요 : 우리는에서 당길 필요가 깊은 코드가 의 '흐름?

+0

액세스 토큰 또는 새로 고침 토큰이 없으면 아니오입니다. Azure AD에서 인증을 보내야합니다. 하지만 캐시에 새로 고침 토큰을 가져야하지 않습니까? 지난 14 일 (IIRC)이므로 리디렉션을 수행 할 필요가 없습니다. – juunas

+0

글쎄, 어디에서 app A 쿠키가 인증되는지를 시나리오를 처리하려고 시도했지만, 어떤 이유로 든 B 리소스에 대한 액세스/새로 고침 토큰이 유효하지 않거나 삭제되었습니다. 재 인증을 위해 리디렉션하면 쿠키에서 가져와 액세스 토큰없이 앱 A에 서명하는 패스 스루가 허용됩니다. 쿠키를 삭제하고 리디렉션하여 처리해야합니까? – cjsmith

답변

2

액세스 토큰과 갱신 토큰을 분실 한 경우 사용자를 Azure AD로 리디렉션해야 다시 인증 할 수 있습니다. 그들은 여전히 ​​인증을 받아서 인증 코드와 함께 앱으로 리디렉션됩니다. 나는이 않는 예외 필터를 만들어 내 프로젝트 중 하나에서

:

public void OnException(ExceptionContext filterContext) 
{ 
    //If the error is a silent token acquisition exception from ADAL.. 
    if(filterContext.Exception is AdalSilentTokenAcquisitionException) 
    { 
     //Instead of the usual procedure, return a 401 which triggers the OpenIdConnect middleware redirection 
     filterContext.Result = new HttpUnauthorizedResult(); 
     filterContext.ExceptionHandled = true; 
    } 
} 

예외는 침묵 토큰 획득에 실패 곳, 바로 오류를 삼켜, 401로 결과를 변경 슬로우됩니다 그렇다면 어떤 OpenIdConnect 미들웨어가 사용자를 Azure AD로 보내도록 트리거합니다.

AutomaticAuthenticate=true이 있으므로이 작업을 수행해야합니다.

+0

HttpUnauthorizedResult는 ASP.NET Core MVC의 UnauthorizedResult 여야합니다. – KorsG

관련 문제