OptionsConfigurationServiceCollectionExtensions add options
KK
191
Reputation points
Hi,
I would like to change the OnTokenValidated event in authentication.
But do not know how to use the BinderOptions in OptionsConfigurationServiceCollectionExtensions.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.Unspecified;
// Handling SameSite cookie according to https://learn.microsoft.com/en-us/aspnet/core/security/samesite?view=aspnetcore-3.1
options.HandleSameSiteCookieCompatibility();
});
// Configuration to sign-in users with Azure AD B2C
services.AddMicrosoftIdentityWebAppAuthentication(Configuration, "AzureAdB2C");
services.AddControllersWithViews()
.AddMicrosoftIdentityUI();
services.AddRazorPages();
//Configuring appsettings section AzureAdB2C, into IOptions
services.AddOptions();
services.Configure<OpenIdConnectOptions>(Configuration.GetSection("AzureAdB2C"));
}
Say, if I want to change the sample code above to this one. Looks like I can't directly do this.
services.Configure<OpenIdConnectOptions>(Configuration.GetSection("AzureAdB2C"), options => {
options.TokenValidationParameters.RoleClaimType = "roles";
options.Events.OnTokenValidated = (ctx) =>
{
var redirectUri = new Uri(ctx.Properties.RedirectUri, UriKind.RelativeOrAbsolute);
if (redirectUri.IsAbsoluteUri)
{
ctx.Properties.RedirectUri = redirectUri.PathAndQuery;
}
var claimsIdentity = ctx.Principal?.Identity as ClaimsIdentity;
claimsIdentity?.AddClaim(new Claim(ClaimTypes.Role, "Admins"));
ServiceLocator.Current.GetInstance<ISynchronizingUserService>().SynchronizeAsync(claimsIdentity);
return Task.FromResult(0);
};
});