首页 > 解决方案 > Microsoft.Identity.Web AddMicrosoftIdentityWebApp - 无法设置 AccessDeniedPath

问题描述

Microsoft.Identity.Web在 .NET core 3.1 应用程序中使用该包(需要 LTS)。

我可以按照文档进行这项工作,但是我可以设置AccessDeniedPath成功,但登录不会持续存在,或者路径没有设置。

我在这里尝试了这两种方法: https ://github.com/AzureAD/microsoft-identity-web/wiki/SameSite-Cookies

我还有通过外部方案登录的本地用户/角色。

我的启动目前看起来像:

添加标准身份

services.AddDefaultIdentity<ApplicationUser>()
                .AddRoles<ApplicationRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>();

添加 Azure ID

var azureAdConfig = Configuration.GetSection("AzureAD");
            services.AddAuthentication()
                .AddMicrosoftIdentityWebApp(options =>
                {
                    options.ClientId = azureAdConfig["ClientId"];
                    options.TenantId = azureAdConfig["TenantId"];
                    options.Domain = azureAdConfig["Domain"];
                    options.Instance = azureAdConfig["Instance"];
                    options.CallbackPath = azureAdConfig["CallbackPath"];
                    options.SignedOutCallbackPath = azureAdConfig["SignedOutCallbackPath"];

                    //options.AccessDeniedPath = new PathString("/Account/AccessDenied");
                }, options =>
                {
                    options.AccessDeniedPath = new PathString("/Account/AccessDenied");
                });

如果我更改services.AddAuthentication()为,services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)则重定向有效,但是用户登录不会持续存在。

该代码将获取用户,从以下位置成功返回: var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: rememberMe);但登录并没有继续让我认为这是一个 cookie 问题、命名错误等。

是我想要做的吗?或者我应该使用AddMicrosoft身份验证并只检查域?由于此方法仅适用于公共端点(从我能找到的)。

标签: c#asp.net-coreasp.net-identity

解决方案


推荐阅读