将 Azure AD 集成到 ASP.NET Core Web 应用程序时更改默认拒绝访问路径

mga*_*lpy 3 c# asp.net-core

我正在尝试在使用 Azure AD 时拒绝授权时更改默认访问被拒绝路径。

例如,在使用 Microsoft 的“将 Azure AD 集成到 ASP.NET Core Web 应用程序”示例时,请参见此处:https : //azure.microsoft.com/en-us/resources/samples/active-directory-dotnet-webapp -openidconnect-aspnetcore/

文章引用了 GitHub 上的示例项目,请参见此处:https : //github.com/Azure-Samples/active-directory-dotnet-webapp-openidconnect-aspnetcore

我在配置内部选项Startup.cs以更改拒绝访问的默认控制器/方法(即“Account/AccessDenied”)时遇到困难。

有人可以帮助提供对上面的 github 示例项目所需的更改,以便未经授权的用户在被拒绝授权而不是默认的“Account/AccessDenied”时被带到不同的路径?

更新:我在我的项目中添加了@Brad 之前(和现在再次)在启动中建议的内容,但它没有改变,而且我仍然被定向到“Account/AccessDenied”......你能想到任何其他设置可能会管理这个?

对于我的项目(在 Visual Studio 2017 中使用工作或学校帐户身份验证自动创建的 ASP.NET Core Web 应用程序 - Web 应用程序(模型-视图-控制器)),与示例项目不同。我正在引用 NuGet 包Microsoft.AspNetCore.Authentication.AzureAD.UI并按以下方式设置我的 AzureAD(请注意使用.AddAzureAD和 不.AddAzureAd):

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.None;
});

services
    .AddAuthentication(AzureADDefaults.AuthenticationScheme)
    .AddAzureAD(options => Configuration.Bind("AzureAd", options))
    .AddCookie(options =>
    {
        options.AccessDeniedPath = "/Home";
     });
Run Code Online (Sandbox Code Playgroud)

Mic*_*eld 11

如果您使用AddAzureAd仅执行动作 lambda的简单重载,库会自动为您添加 Cookie 方案,但它会将其添加到“AzureAdDefaults.CookieScheme”名称下(不知道为什么),并带有自己的一组选项。如果您尝试使用任何常规方法来自定义 cookie 选项,它将永远不会被调用,因为您正在尝试配置错误的 cookie 方案。

相反,您可以在添加 Azure AD 自定义 cookie 方案后为其配置 cookie 选项,如下所示:

services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
    .AddAzureAD(options => Configuration.Bind("AzureAd", options));

services.Configure<CookieAuthenticationOptions>(AzureADDefaults.CookieScheme, options => options.AccessDeniedPath = "/Home/NoAuth");
Run Code Online (Sandbox Code Playgroud)