Redirect Microsoft Identity Platform Access Denied in ASP.NET Core

I feel like I’ve read every post on Stack Overflow and nothing is matching my scenario. I’ve got an ASP.NET Core website that uses Microsoft Identity Platform for authentication. This is set up by calling:

services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
        .AddMicrosoftIdentityWebApp(options => ...)

Then, I’m using role-based claims to authorize who is allowed to access the website. When the user authenticates, I populate their roles with the security groups they are a member of (using Graph to look up group membership).

Here is the complete block for authentication:

// Setup Graph and authentication
private static void ConfigureAuthentication(
   IServiceCollection services,
   ConfigurationManager configuration)
{
   var initialScopes =
      configuration["DownstreamApi:Scopes"]?.Split(' ') ??
      configuration["MicrosoftGraph:Scopes"]?.Split(' ');

   services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
     .AddMicrosoftIdentityWebApp(options =>
     {
         configuration.Bind("AzureAd", options);
         options.TokenValidationParameters.RoleClaimType = "groups";
         options.AccessDeniedPath = "/Error/AccessDenied";
         options.ErrorPath = "/Error/Error";

         options.Events.OnTokenValidated = async context =>
         {
            if(context != null)
            {
               var allGroupIds = GetConfigurationGroups(configuration);
               await ClaimHelpers.PopulateGroupClaims(context, allGroupIds);
            }
         };
      })
      .EnableTokenAcquisitionToCallDownstreamApi(options =>
         configuration.Bind("AzureAd", options), initialScopes)
      .AddMicrosoftGraph(configuration.GetSection("MicrosoftGraph"))
      .AddInMemoryTokenCaches();

   services.AddControllersWithViews().AddMicrosoftIdentityUI();
}

// Populate the authorization policies and set the default.
private static void ConfigureGroupAuthorization(
   IServiceCollection services,
   ConfigurationManager configuration)
{
   services.AddAuthorization(p =>
   {
      p.AddPolicy(PolicyNames.PortalUser, policy =>
      {
         policy.RequireAuthenticatedUser();
         policy.RequireRole(configuration["Groups:PortalUserGroupID"]!);
      });
   });

   services.AddAuthorization(options =>
   {
      options.FallbackPolicy = options.GetPolicy(PolicyNames.PortalUser);
      options.DefaultPolicy = options.GetPolicy(PolicyNames.PortalUser)!;
   });
}

Everything works fine except when someone does not have the correct claim. Then, they are redirected to https://localhost:44321/MicrosoftIdentity/Account/AccessDenied?ReturnUrl=%2F.

This is not what I want. I want unauthorized users to be directed my custom /Error/AccessDenied action. I don’t want them directed to the default Microsoft Identity callback.

I need to tell my users which security group they need to join if they can’t access the page. What do I need to change or configure so users are directed to /Error/AccessDenied instead of Microsoft Identity? Do I need to change something in my Azure App Registration?

If any of my terminology is wrong, please correct it. I’m still learning ASP.NET Core.

I’ve read many similar posts.

This is probably the most similar post but doesn’t achieve the redirect: How to override default Identity AccessDenied route in ASP.NET CORE MVC

Checked with our senior engineer and he’s unfamiliar with the problem.
Tried asking ChatGPT.

New contributor

David is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật