【问题标题】:.Net Core 5.0 Identity Scaffolded Code not working with Windows Authentication.Net Core 5.0 Identity Scaffolded 代码不适用于 Windows 身份验证
【发布时间】:2021-07-20 16:41:38
【问题描述】:

我有一个当前使用 Windows 身份验证的 .net Core 5.0 MVC 应用程序。我想向应用程序添加角色,因此决定使用 Identity 框架,然后复制站点的用户并基本上为他们分配本地角色。

这被证明是非常棘手的,我最终找到了一个中途解决方案,我有一个使用 Windows 身份验证的可见身份验证用户,我可以使用它通过 UserManager 和 RoleManager 访问用户和角色表。但是,方法修饰不适用于我的本地角色,并且重定向到区域中文件夹中的 AccessDenied 等文件也不起作用。 所以

var identityUser = _userManager.FindByNameAsync(User.Identity.Name).Result;
        var roles = _userManager.GetRolesAsync(identityUser).Result;
        var isAdmin = _userManager.IsInRoleAsync(identityUser, "Admin").Result;
        if (isAdmin)
            return Forbid();

抓住我

No webpage was found for the web address: https://localhost:44312/Account/AccessDenied?ReturnUrl=%2FHome

这是在 IdentityHostingStartup.cs 中创建的脚手架代码

public class IdentityHostingStartup : IHostingStartup
{
    public void Configure(IWebHostBuilder builder)
    {
        builder.ConfigureServices((context, services) => {
            services.AddDbContext<IdentityContext>(options =>
                options.UseSqlServer(
                    context.Configuration.GetConnectionString("YWAFTraceConnection")));

            services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = false)
                .AddRoles<IdentityRole>()
                .AddEntityFrameworkStores<IdentityContext>()
                .AddDefaultUI();

            services.AddAuthorization(options =>
            {
                options.FallbackPolicy = new AuthorizationPolicyBuilder()
                    .RequireAuthenticatedUser()
                    .Build();
            });

            services.ConfigureApplicationCookie(options =>
            {
                options.AccessDeniedPath = new Microsoft.AspNetCore.Http.PathString("/Account/AccessDenied");                    
            });
        });
    }

AccessDeniedPath 的路径条目组合似乎不起作用 - 我得到的只是 404 - 我可以解决缺少装饰的问题,但确实需要触发拒绝访问页面。

这是我的区域文件夹的结构

[![区域文件夹结构][1]][1]

在本地使用 IIS Express 或在 IIS 上部署时都无法使用

【问题讨论】:

  • 你是否在 IIS 服务器中启用了 windows 身份验证?比如
  • 是的,谢谢 - 我认为问题在于我正在尝试同时使用 WA 和 Identity。所以我有一个包含所有 Windows 帐户属性的 User 对象和一个引用我的本地角色的单独 IdentityUser。

标签: asp.net-core asp.net-identity .net-5


【解决方案1】:

app.UseEndpoints 中添加endpoints.MapRazorPages(); Startup.cs 解决了找不到区域页面的问题。

如优秀教程here中所述,通过实施自定义策略来修复控制器和方法身份验证

【讨论】:

    猜你喜欢
    • 2017-06-26
    • 1970-01-01
    • 2017-09-11
    • 1970-01-01
    • 2019-11-28
    • 2010-11-05
    • 1970-01-01
    • 2021-10-18
    • 2020-03-28
    相关资源
    最近更新 更多