Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev/zahalzel/b2c graph fix on rel 6 #1979

Merged
merged 2 commits into from Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -79,7 +79,7 @@ public class MicrosoftIdentityPlatformApplicationManager
.AddAsync(servicePrincipal).ConfigureAwait(false);

// B2C does not allow user consent, and therefore we need to explicity grant permissions
if (applicationParameters.IsB2C)
if (applicationParameters.IsB2C && applicationParameters.CallsDownstreamApi) // TODO need to have admin permissions for the downstream API
{
IEnumerable<IGrouping<string, ResourceAndScope>>? scopesPerResource = await AddApiPermissions(
applicationParameters,
Expand Down Expand Up @@ -114,6 +114,11 @@ public class MicrosoftIdentityPlatformApplicationManager
return null;
}

if (applicationParameters.IsB2C)
{
createdApplication.AdditionalData.Add("IsB2C", true);
}

ApplicationParameters? effectiveApplicationParameters = GetEffectiveApplicationParameters(tenant!, createdApplication, applicationParameters);

// Add password credentials
Expand Down
Expand Up @@ -125,6 +125,15 @@ internal async Task<string> PrintApplicationsList()

if (applicationList.Any())
{
Organization? tenant = await GetTenant(GraphServiceClient);
if (tenant != null && tenant.TenantType.Equals("AAD B2C", StringComparison.OrdinalIgnoreCase))
{
foreach (Application app in applicationList)
{
app.AdditionalData.Add("IsB2C", true);
}
}

//order list by created date.
applicationList = applicationList.OrderByDescending(app => app.CreatedDateTime).ToList();

Expand All @@ -150,6 +159,40 @@ internal async Task<string> PrintApplicationsList()
return outputJsonString;
}

private static async Task<Organization?> GetTenant(GraphServiceClient graphServiceClient)
{
Organization? tenant = null;
try
{
tenant = (await graphServiceClient.Organization
.Request()
.GetAsync()).FirstOrDefault();
}
catch (ServiceException ex)
{
if (ex.InnerException != null)
{
Console.WriteLine(ex.InnerException.Message);
}
else
{
if (ex.Message.Contains("User was not found") || ex.Message.Contains("not found in tenant"))
{
Console.WriteLine("User was not found.\nUse both --tenant-id <tenant> --username <username@tenant>.\nAnd re-run the tool.");
}
else
{
Console.WriteLine(ex.Message);
}
}

Environment.Exit(1);
}

return tenant;
}


internal async Task<string> PrintServicePrincipalList()
{
string outputJsonString = string.Empty;
Expand Down