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

Integration with asp.net core SSO with Single Tenant SPN #9

Closed
2 of 9 tasks
ellio246t opened this issue Sep 6, 2023 · 10 comments
Closed
2 of 9 tasks

Integration with asp.net core SSO with Single Tenant SPN #9

ellio246t opened this issue Sep 6, 2023 · 10 comments

Comments

@ellio246t
Copy link

Sample

https://github.com/pnp/mgt-samples/tree/main/samples/app/msal2provider-asp-net-core-sso

Author(s)

@sebastienlevert

What happened?

I have followed the instructions in the sample with two deviations:

  1. My SPN is a single tenant SPN
  2. The client secret is not in app.config

The sdk controls never manage to display data from AAD and appear as empty or with a message no data found.
How can I get this to work with a single tenant SPN

App Registration

Here are the app permissions:
image

Client Secret

This is supplied in Program.cs after retrieving from key vault. SSO is working fine:
var secretClient = new SecretClient(new Uri(builder.Configuration.GetValue("KeyVault:VaultUri")), new DefaultAzureCredential());
var clientSecret = secretClient.GetSecret(builder.Configuration.GetSection("AzureAD").GetValue("ApplicationName"));
var initialScopes = builder.Configuration["DownstreamApi:Scopes"]?.Split(' ') ?? builder.Configuration["MicrosoftGraph:Scopes"]?.Split(' ');

builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(microsoftIdentityOptions =>
{
microsoftIdentityOptions.ClientId = builder.Configuration.GetSection("AzureAd").GetValue("ClientId");
microsoftIdentityOptions.Domain = builder.Configuration.GetSection("AzureAd").GetValue("Domain");
microsoftIdentityOptions.TenantId = builder.Configuration.GetSection("AzureAd").GetValue("TenantId");
microsoftIdentityOptions.Instance = builder.Configuration.GetSection("AzureAd").GetValue("Instance");
microsoftIdentityOptions.CallbackPath = builder.Configuration.GetSection("AzureAd").GetValue("CallbackPath");
microsoftIdentityOptions.ClientSecret = clientSecret.Value.Value;
microsoftIdentityOptions.GetClaimsFromUserInfoEndpoint = true;
})
.EnableTokenAcquisitionToCallDownstreamApi(initialScopes)
.AddMicrosoftGraph(builder.Configuration.GetSection("MicrosoftGraph"))
.AddInMemoryTokenCaches();

MSAL2 Provider Initialization

This is happening in _layout.cshtml

@if (User.Identity.IsAuthenticated)
{


<script src="https://unpkg.com/@@microsoft/mgt@3/dist/bundle/mgt-loader.js"></script>
<script>
mgt.Providers.globalProvider = new mgt.Msal2Provider({
clientId: "@configuration["AzureAd:ClientId"]",
loginHint: "@User.Claims.FirstOrDefault(c => c.Type == "preferred_username")?.Value",
redirectUri: "/sso.html",
authority: "https://login.microsoftonline.com/@Configuration["AzureAd:TenantId"]"
loginType: mgt.LoginType.Popup
});
</script>
}
If I inspect the html I see this the the header:

<script src="https://unpkg.com/@microsoft/mgt@3/dist/bundle/mgt-loader.js"></script>

--
  | <script>
  | mgt.Providers.globalProvider = new mgt.Msal2Provider({
  | clientId: "aa1c150d-0ab5-44ee-b1b3-48150574f0d1",
  | loginHint: "Philip.Elliott@**********.com",
  | redirectUri: "/sso.html",
  | authority: "https://login.microsoftonline.com/********-My-TenantId"
  | loginType: mgt.LoginType.Popup
  | });
  | </script>

SSS.html

This is added to wwwroot.

Steps to reproduce

  1. Follow the tutorial as per above
    2.Add this to the home page @if (User.Identity.IsAuthenticated)
    {

    Signed in User:

     <mgt-people-picker></mgt-people-picker>
    

    }

  2. Observe that no data from AAD is displayed.

Expected behavior

I expect to see the logged on user details and be able to search and pick users from AAD.
This is what I see:
image

Developer environment

None

Browsers

  • Internet Explorer
  • Microsoft Edge
  • Google Chrome
  • FireFox
  • Safari
  • mobile (iOS/iPadOS)
  • mobile (Android)
  • not applicable
  • other (enter in the "Additional environment details" area below)

Additional environment details

asp.net core MVC web application hosted in Azure PaaS web app. Here's the project defintion:

netcoreapp6.0 enable enable
@sebastienlevert
Copy link
Collaborator

Can you provide a small repro that we could test it out? That would help us identify the right issues. Thanks! Also adding @gavinbarron as he's been the one fiddling around with this sample!

@github-project-automation github-project-automation bot moved this to Needs Triage 🔍 in Graph Toolkit Sep 7, 2023
@gavinbarron
Copy link
Collaborator

Oh wow, you were so close to having this working @ellio246t

You're missing a comma at the end of the authority line where you set up the provider in the _Layout.cshtml and that's breaking your SSO flow.

When looking at the network requests in the browser your should see a chain of requests out to the login.microsoft.com site and redirecting back to the sso page if you have a valid config for your provider.

Additionally you should see any errors in the query string for the request to the sso page.

@ellio246t
Copy link
Author

Thanks for your responses gentlemen. I fixed the missing comma but that must be issue 1 of N, the behaviour was unchanged. I've run a network trace and could see the chain of requests just as you described. SS0.html is called but the token request has failed:
image

I've created a stripped down version of the app I can share. If you fill in the app.config file and sort the client secret in program.cs it should run.
Web.zip

@ellio246t
Copy link
Author

Hi
I investigated the bad request error some more. The actual error is:
AADSTS9002326: Cross-origin token redemption is permitted only for the 'Single-Page Application' client-type. Request origin: 'https://localhost:7267'.
Trace ID: 6c70a7b3-a32e-41bc-8ade-882a242a0f00
Correlation ID: 9a858aed-9c1f-4a85-9c34-84951069aabb
Timestamp: 2023-09-13 07:40:57Z

So it seems the issue is to do the application registration but what specifically? I already mentioned that the app I'm using is configured as a single tenant app and that is the only thing (I know of) that deviates from the code sample instructions. Will the SDK work with a single tenant app registration and if so how do I configure it?

@gavinbarron
Copy link
Collaborator

gavinbarron commented Sep 13, 2023 via email

@gavinbarron
Copy link
Collaborator

The authentication blade of the app registration should look like this:
Screenshot of the Authentication blade in AAD for an app registration

@ellio246t
Copy link
Author

I added a platform for SPO and now I can see the controls have started to work:
image

This is real progress. Thankyou so much!

I still have one final question for you:
In the image I am being prompted to ask my AD admin to approve API permissions. Read Presence of All users isn't granted yet but view user basic profile is. What is going on here? Is this more missing app registration config? I don't want users of the web app to see such prompts, how do I fix it?

@sebastienlevert
Copy link
Collaborator

To avoid users seeing this prompt, you'll have to do a global admin consent like here: https://learn.microsoft.com/en-us/azure/active-directory/manage-apps/grant-admin-consent?pivots=portal. This will approve the set of permissions you need for all the users in your tenant.

@ellio246t
Copy link
Author

ellio246t commented Sep 15, 2023 via email

@gavinbarron
Copy link
Collaborator

The mechanics of app consent are not something we have control over. MGT is a user of the AAD/Entra services and we just have to live with their rules, I suspect what is happening here is that the admin is being asked to consent for all the requested scopes for the application, not just the additional one that requires admin approval.

As the integration between the sample and the single tenant application is now resolved I think we can close this issue. Please do re-open it, or raise a new issue if there are additional issues you face.

@github-project-automation github-project-automation bot moved this from Needs Triage 🔍 to Done ✔️ in Graph Toolkit Sep 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants