繁体   English   中英

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

[英]Changing default access denied path when integrating Azure AD into an ASP.NET Core web app

我正在尝试在使用 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";
     });

如果你使用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");

如果您使用 Microsoft.Identity.Web 包,就像在最新的 MVC 核心 VS 模板中一样,您将拥有以下行:

services.AddSignIn(Configuration);

在这种情况下,您必须添加以下内容:

services.Configure<CookieAuthenticationOptions>(CookieAuthenticationDefaults.AuthenticationScheme, 
            options => options.AccessDeniedPath = "/your/path");

这是在 cookie 身份验证选项中配置的。

services
    .AddAuthentication(...)
    .AddAzureAd(...)
    .AddCookie(options => 
    {
        options.AccessDeniedPath = "/path/to/unauthorized";
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM