private async Task <AuthenticationResult> CreateAccessToken() { AuthenticationConfig config = AuthenticationConfig.ReadFromEV(); IConfidentialClientApplication app; app = ConfidentialClientApplicationBuilder.Create(config.ClientId) .WithClientSecret(config.ClientSecret) .WithAuthority(new Uri(config.Authority)) .Build(); string[] scopes = new string[] { config.Scope }; AuthenticationResult result = null; try { _logger.LogInfo("Class: AccessTokenService, Method: CreateAccessToken, Info: Acquire Token For Client."); result = await app.AcquireTokenForClient(scopes) .ExecuteAsync(); } catch (MsalUiRequiredException ex) { _logger.LogError($"Class: AccessTokenService, Method: CreateAccessToken, {Environment.NewLine} Exception: {ex}, {Environment.NewLine} Message: {ex.Message}, {Environment.NewLine} StackTrace: {ex.StackTrace}"); return(result); } catch (MsalServiceException ex) { _logger.LogError($"Class: AccessTokenService, Method: CreateAccessToken, {Environment.NewLine} Exception: {ex}, {Environment.NewLine} Message: {ex.Message}, {Environment.NewLine} StackTrace: {ex.StackTrace}"); return(result); } return(result); }
public void ConfigureServices(IServiceCollection services) { AuthenticationConfig config = AuthenticationConfig.ReadFromEV(); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.AddMemoryCache(); services.AddHttpClient(); services.AddHttpClient("protectedapi", options => { options.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); }); services.AddSingleton <ILoggerManager, LoggerManager>(); services.AddSingleton <IAccessTokenService, AccessTokenService>(); services.AddScoped <IProtectedWebApiCallerService, ProtectedWebApiCallerService>(); services.AddTransient <IResourceFetchService, ResourceFetchService>(); services.AddTransient <IPatientService, PatientService>(); services.AddTransient <IObservationService, ObservationService>(); services.AddCors(options => { options.AddPolicy(MyAllowSpecificOrigins, builder => { builder.AllowAnyOrigin() .AllowAnyHeader() .AllowAnyMethod(); }); }); }