/// <summary> /// /// </summary> /// <param name="services"></param> /// <param name="name"></param> /// <param name="configuration"></param> /// <param name="events"></param> public static void AddAzureAdAuthentication(this IServiceCollection services, IConfiguration configuration, JwtBearerEvents events = null, string sectionName = "AzureAdAuth") { var azureAdAuth = new AzureAdAuth(); configuration.Bind(sectionName, azureAdAuth); services.AddAuthentication(sharedoptions => { sharedoptions.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(options => { options.Authority = azureAdAuth.Authority; options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters { ValidAudiences = azureAdAuth.ValidAudiences, ValidIssuers = azureAdAuth.ValidIssuers }; if (events != null) { options.Events = events; } }); services.AddScoped <ITokenService, TokenService>(); }
// Login to the user's account and create a UCWA app static void Login() { #region Login // Clear any cached tokens. // We do this to ensure logins with different accounts work // during the same launch of the app authenticationContext.TokenCache.Clear(); Console.WriteLine("How do you want to login?"); Console.WriteLine("console | dialog | code >"); string loginStyle = Console.ReadLine(); AuthenticationResult testCredentials = null; UserCredential uc = null; switch (loginStyle.ToLower()) { case "console": uc = GetUserCredentials(); testCredentials = UcwaSfbo.AzureAdAuth.GetAzureAdToken(authenticationContext, sfboResourceAppId, clientId, redirectUri, uc); break; case "dialog": if (redirectUri == String.Empty) { Console.WriteLine("You haven't defined redirectUri which is needed if you want to sign in with a dialog"); return; } testCredentials = UcwaSfbo.AzureAdAuth.GetAzureAdToken(authenticationContext, sfboResourceAppId, clientId, redirectUri, uc); break; case "code": DeviceCodeResult deviceCodeResult = authenticationContext.AcquireDeviceCodeAsync(sfboResourceAppId, clientId).Result; Console.WriteLine(deviceCodeResult.Message); Console.WriteLine("Or, use Control-C to exit the app"); testCredentials = authenticationContext.AcquireTokenByDeviceCodeAsync(deviceCodeResult).Result; break; default: Console.Write("Please select a login style and try again"); Console.Write("\n"); return; } if (testCredentials == null) { Console.WriteLine("We encountered an Azure AD error"); Console.WriteLine("Check your tenant, clientID, and credentials"); return; } ucwaApplicationsUri = UcwaAutodiscovery.GetUcwaRootUri(authenticationContext, sfboResourceAppId, clientId, redirectUri, uc); Console.WriteLine("We'll store the base UCWA app URI for use with UCWA app calls"); Console.WriteLine("We prefix this to the links returned from the UCWA apps POST"); Console.WriteLine("Since these links aren't full URIs"); ucwaApplicationsHost = Helpers.ReduceUriToProtoAndHost(ucwaApplicationsUri); Console.WriteLine("ucwaApplicationsHost is " + ucwaApplicationsHost); Console.WriteLine("Get a token to access the user's UCWA Applications Resources from Azure AD."); Console.WriteLine("We can re-use this token for each UCWA app call"); ucwaAuthenticationResult = AzureAdAuth.GetAzureAdToken(authenticationContext, ucwaApplicationsHost, clientId, redirectUri, uc); Console.WriteLine("Now we'll create and/or query UCWA Apps via POST"); Console.WriteLine("Well create a UCWA apps object to pass to CreateUcwaApps"); UcwaApplications.UcwaMyAppsObject ucwaMyAppsObject = new UcwaApplications.UcwaMyAppsObject() { UserAgent = "myAgent", EndpointId = "1234", Culture = "en-US" }; Console.WriteLine("Making request to ucwaApplicationsUri " + ucwaApplicationsUri); createUcwaAppsResults = UcwaApplications.CreateUcwaApps(ucwaAuthenticationResult, ucwaApplicationsUri, ucwaMyAppsObject); return; }