Skip to content

Commit e36935f

Browse files
author
Kalyan Krishna
committed
Minor fixes
1 parent 709dafe commit e36935f

File tree

5 files changed

+48
-8
lines changed

5 files changed

+48
-8
lines changed

5-WebApp-AuthZ/5-2-Groups/AppCreationScripts/Configure.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ Function ConfigureApplications
192192
-ReplyUrls "https://localhost:44321/", "https://localhost:44321/signin-oidc", "https://localhost:44321/Account/EndSession" `
193193
-IdentifierUris "https://$tenantName/WebApp-GroupClaims" `
194194
-PasswordCredentials $key `
195-
-Oauth2AllowImplicitFlow $true `
196195
-GroupMembershipClaims "SecurityGroup" `
196+
-Oauth2AllowImplicitFlow $true `
197197
-PublicClient $False
198198

199199
$currentAppId = $webAppAadApplication.AppId
@@ -219,7 +219,7 @@ Function ConfigureApplications
219219
# Add Required Resources Access (from 'webApp' to 'Microsoft Graph')
220220
Write-Host "Getting access from 'webApp' to 'Microsoft Graph'"
221221
$requiredPermissions = GetRequiredPermissions -applicationDisplayName "Microsoft Graph" `
222-
-requiredDelegatedPermissions "Directory.Read.All" `
222+
-requiredDelegatedPermissions "User.Read|Directory.Read.All" `
223223

224224
$requiredResourcesAccess.Add($requiredPermissions)
225225

5-WebApp-AuthZ/5-2-Groups/AppCreationScripts/sample.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"RequiredResourcesAccess": [
2626
{
2727
"Resource": "Microsoft Graph",
28-
"DelegatedPermissions": [ "Directory.Read.All" ]
28+
"DelegatedPermissions": [ "User.Read", "Directory.Read.All" ]
2929
}
3030
]
3131
}

5-WebApp-AuthZ/5-2-Groups/Startup.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.AspNetCore.Authorization;
1+
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
2+
using Microsoft.AspNetCore.Authorization;
23
using Microsoft.AspNetCore.Builder;
34
using Microsoft.AspNetCore.Hosting;
45
using Microsoft.AspNetCore.Http;
@@ -44,8 +45,11 @@ public void ConfigureServices(IServiceCollection services)
4445
// This flag ensures that the ClaimsIdentity claims collection will be built from the claims in the token
4546
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
4647

48+
Microsoft.IdentityModel.Logging.IdentityModelEventSource.ShowPII = true;
49+
4750
// Sign-in users with the Microsoft identity platform
48-
services.AddAzureAdV2Authentication(Configuration)
51+
OpenIdConnectOptions openIdConnectOptions = new OpenIdConnectOptions();
52+
services.AddMicrosoftIdentityPlatformAuthentication(Configuration, openIdConnectOptions)
4953
.AddMsal(new string[] { "User.Read", "Directory.Read.All" })
5054
.AddSessionTokenCaches();
5155

Microsoft.Identity.Web/Resource/AadIssuerValidator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3535
namespace Microsoft.Identity.Web.Resource
3636
{
3737
/// <summary>
38-
/// Generic class that validates token issuer from the provided Azure AD authority. Use the <see cref="AadIssuerValidatorFactory"/> to create instaces of this class.
38+
/// Generic class that validates token issuer from the provided Azure AD authority. Use the <see cref="AadIssuerValidatorFactory"/> to create instances of this class.
3939
/// </summary>
4040
public class AadIssuerValidator
4141
{

Microsoft.Identity.Web/StartupHelpers.cs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,45 @@ public static class StartupHelpers
4444
/// <param name="services">Service collection to which to add this authentication scheme</param>
4545
/// <param name="configuration">The Configuration object</param>
4646
/// <returns></returns>
47-
public static IServiceCollection AddMicrosoftIdentityPlatformAuthentication(this IServiceCollection services, IConfiguration configuration)
47+
public static IServiceCollection AddMicrosoftIdentityPlatformAuthentication(this IServiceCollection services, IConfiguration configuration, OpenIdConnectOptions openIdConnectOptions, string configBinderKey = "AzureAd")
4848
{
49-
return AddAzureAdV2Authentication(services, configuration);
49+
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
50+
.AddAzureAD(options => configuration.Bind(configBinderKey, options));
51+
52+
openIdConnectOptions.Authority = openIdConnectOptions.Authority + "/v2.0/";
53+
openIdConnectOptions.TokenValidationParameters.IssuerValidator = AadIssuerValidator.GetIssuerValidator(openIdConnectOptions.Authority).Validate;
54+
openIdConnectOptions.TokenValidationParameters.NameClaimType = "preferred_username";
55+
56+
openIdConnectOptions.Events.OnRedirectToIdentityProvider = context =>
57+
{
58+
var login = context.Properties.GetParameter<string>(OpenIdConnectParameterNames.LoginHint);
59+
if (!string.IsNullOrWhiteSpace(login))
60+
{
61+
context.ProtocolMessage.LoginHint = login;
62+
context.ProtocolMessage.DomainHint = context.Properties.GetParameter<string>(OpenIdConnectParameterNames.DomainHint);
63+
64+
// delete the login_hint and domainHint from the Properties when we are done otherwise
65+
// it will take up extra space in the cookie.
66+
context.Properties.Parameters.Remove(OpenIdConnectParameterNames.LoginHint);
67+
context.Properties.Parameters.Remove(OpenIdConnectParameterNames.DomainHint);
68+
}
69+
70+
// Additional claims
71+
if (context.Properties.Items.ContainsKey(OidcConstants.AdditionalClaims))
72+
{
73+
context.ProtocolMessage.SetParameter(OidcConstants.AdditionalClaims,
74+
context.Properties.Items[OidcConstants.AdditionalClaims]);
75+
}
76+
77+
return Task.FromResult(0);
78+
};
79+
80+
services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
81+
{
82+
options = openIdConnectOptions;
83+
});
84+
85+
return services;
5086
}
5187

5288
/// <summary>

0 commit comments

Comments
 (0)