2017-10-31 9 views
2

우리는 Azure AD B2C와 OAuth를 사용하여 애플리케이션을 설정했지만이 서비스는 정상적으로 작동하지만 서비스 호출로 서비스를 인증하기 위해 노력하고 있습니다. 나는 약간 새로운데, Pluralsight에서 "정상적인"Azure Active Directory에서이 작업을 수행하는 방법에 대한 몇 가지 과정을 수행했지만 제대로 작동 할 수 있지만 B2C와 동일한 원칙을 따르면 작동하지 않습니다. Azure AD B2C로 서비스 인증하기

나는이 빠른 콘솔 응용 프로그램이 있습니다

class Program 
{ 
    private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"]; //APIClient ApplicationId 
    private static string appKey = ConfigurationManager.AppSettings["ida:appKey"]; //APIClient Secret 
    private static string aadInstance = ConfigurationManager.AppSettings["ida:aadInstance"]; //https://login.microsoftonline.com/{0} 
    private static string tenant = ConfigurationManager.AppSettings["ida:tenant"]; //B2C Tenant 
    private static string serviceResourceId = ConfigurationManager.AppSettings["ida:serviceResourceID"]; //APP Id URI For API 
    private static string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant); 

    private static AuthenticationContext authContext = new AuthenticationContext(authority); 
    private static ClientCredential clientCredential = new ClientCredential(clientId, appKey); 

    static void Main(string[] args) 
    { 
     AuthenticationResult result = authContext.AcquireToken(serviceResourceId, clientCredential); 
     Console.WriteLine("Authenticated succesfully.. making HTTPS call.."); 

     string serviceBaseAddress = "https://localhost:44300/"; 
     HttpClient httpClient = new HttpClient(); 
     httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken); 
     HttpResponseMessage response = httpClient.GetAsync(serviceBaseAddress + "api/location?cityName=dc").Result; 

     if (response.IsSuccessStatusCode) 
     { 
      string r = response.Content.ReadAsStringAsync().Result; 
      Console.WriteLine(r); 
     } 
    } 
} 

을 그리고 서비스는 다음과 같이 고정 :

내 B2C 세입자에
private void ConfigureAuth(IAppBuilder app) 
    { 
     var azureADBearerAuthOptions = new WindowsAzureActiveDirectoryBearerAuthenticationOptions 
     { 
      Tenant = ConfigurationManager.AppSettings["ida:Tenant"], 
      TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters() 
      { 
       ValidAudience = ConfigurationManager.AppSettings["ida:Audience"] 
      } 
     }; 

     app.UseWindowsAzureActiveDirectoryBearerAuthentication(azureADBearerAuthOptions); 
    } 

나는이 같은 꽤 많이 설치 두 개의 서로 다른 응용 프로그램이 있습니다

Azure B2C app setup 두 응용 프로그램 모두 "키"옵션에서 오는 비밀로 설정되었습니다. 생성 된 키는 Azure Active Directory를 사용하는 경우와 약간 다르게 구성됩니다.

토큰을 성공적으로 가져올 수 있지만 다른 서비스에 연결하려고하면 401이 표시됩니다. Azure Active Directory에 비해 B2C를 사용할 때 인증면에서 다른 것을해야합니까? API를 응용 프로그램에 웹 또는 네이티브 응용 프로그램의 접근에 대한 액세스 토큰을 발행 할 수

+0

[헤드리스 인증 Azure AD b2c] (https://stackoverflow.com/questions/35072371/headless-authentication-azure-ad-b2c)의 가능한 복제본 – spottedmahn

답변

3

하늘빛의 Active Directory B2C 경우 :

  1. B2C에 등록되어 이러한 애플 리케이션 모두;
  2. 액세스 토큰은 대화 형 사용자 흐름 (즉, 인증 코드 또는 암시 적 플로우)의 결과로 발행됩니다.

현재 데몬이나 서버 응용 프로그램에서 API 응용 프로그램 (예 : 클라이언트 자격증 명 흐름)에 액세스하기 위해 액세스 토큰을 발행해야하는 특정 시나리오는 지원되지 않습니다. B2C 거주자 용 "App Registrations"블레이드를 통해이 두 가지 응용 프로그램을 모두 등록 할 수 있습니다. 이 API 응용 프로그램은 웹/네이티브 응용 프로그램뿐만 아니라 데몬/서버 응용 프로그램 모두에서 토큰을받을 경우,

https://feedback.azure.com/forums/169401-azure-active-directory/suggestions/18529918-aadb2c-support-oauth-2-0-client-credential-flow

:

당신은 클라이언트 자격 증명에 대한 지원을 찬성 투표 수는에서 B2C로 흐름 두 개의 토큰 발급자 (B2C 및 B2C 거주자의 Azure AD 디렉토리)에서 토큰의 유효성을 검사하도록 API 응용 프로그램을 구성해야합니다.

+2

이것을 확인해 주셔서 감사합니다. 어젯밤에 같은 정보를 발견하고 B2C 테넌트의 App 등록 블레이드를 통해 작동하게 만들었지 만 B2C 앱에서 직접 가져 오는 것이 더 쉽습니다. – ruffen

+0

@ruffen - 어떻게했는지에 대한 더 많은 단서가 있습니까? – ubienewbie

+0

@ubienewbie [이 SO 게시물] 참조 (https://stackoverflow.com/a/49036907/185123) – spottedmahn