question

KunLi-4284 avatar image
0 Votes"
KunLi-4284 asked MarileeTurscak-MSFT commented

OptionsConfigurationServiceCollectionExtensions add options

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://docs.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);
             };
         });
dotnet-aspnet-core-generaldotnet-aspnet-core-mvc
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Could you please provide more details around which sample you are following, which error you are receiving, and which objective you would like to achieve? There is an example here of overriding OnTokenValidated. thttps://stackoverflow.com/questions/50756478/overriding-ontokenvalidated-jwtbearerevents-with-custom-function-net-core-2

0 Votes 0 ·

0 Answers