【问题标题】:Getting 401 Unauthorized with MVC Pages while Identity Razor pages work as expected在 Identity Razor 页面按预期工作时,使用 MVC 页面获得 401 Unauthorized
【发布时间】:2022-02-17 12:10:56
【问题描述】:

背景

我正在做一个 POC,以了解 Angular、Razor 和 MVC 页面是否在 Web 应用程序中无缝工作。我从名为“ASP.NET Core with Angular”的 Visual Studio 模板开始。我选择了“个人帐户”以包含默认身份验证功能。这将创建一个带有安全 Web API 端点 (WeatherForecast) 的 Angular 应用程序,并提供基本的用户注册、登录、注销、用户个人资料页面等内置功能。到目前为止,当我尝试从受保护的 API (WeatherForecast) 获取数据时一切正常) 我被重定向到我可以登录的身份/帐户/登录剃须刀页面,然后被重定向回 Angular,我可以看到返回的数据和填充的网格。至此一切正常。

问题

我添加了一个带有基本“Hello World”HTML 视图的 DemoController 类。当我尝试使用 /demo 访问这个新页面时,它按预期工作。但是,当我将 [Authorize] 属性应用于控制器时,我得到 401 Unauthorized。我在服务器端检查了 User.IsAuthenticated 属性设置为 false 尽管之前已成功登录。现在有趣的观察是用户个人资料页面(受保护并且仅在有活动登录时才有效)工作正常。

请注意,来自 Angular 的所有 API 调用问题都使用 JWT 不记名令牌并且工作正常。当我尝试访问用户个人资料页面时,它不使用 JWT,它使用 cookie 进行身份验证。对 /demo 页面的 GET 请求在标头中也包含所有这些 cookie,但仍会遇到 401。

我花了很多时间浏览文章,在网上搜索但没有成功。我们发现的最后一件事是:ASP.NET Core 5.0 JWT authentication is throws 401 code 但这也无济于事。

该项目是使用 Visual Studio 2022、.net core 6.0 创建的。这是 Program.cs 文件供您参考:

    using CoreAngular.Data;
    using CoreAngular.Models;
    using Microsoft.AspNetCore.Authentication;
    using Microsoft.AspNetCore.Identity;
    using Microsoft.AspNetCore.Identity.UI;
    using Microsoft.EntityFrameworkCore;

    var builder = WebApplication.CreateBuilder(args);

    // Add services to the container.
    var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
    builder.Services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(connectionString));
    builder.Services.AddDatabaseDeveloperPageExceptionFilter();

    builder.Services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
        .AddEntityFrameworkStores<ApplicationDbContext>();

    builder.Services.AddIdentityServer()
        .AddApiAuthorization<ApplicationUser, ApplicationDbContext>();

    builder.Services.AddAuthentication()
        .AddIdentityServerJwt();

    builder.Services.AddControllersWithViews();
    builder.Services.AddRazorPages();

    var app = builder.Build();

    // Configure the HTTP request pipeline.
    if (app.Environment.IsDevelopment())
    {
        app.UseMigrationsEndPoint();
    }
    else
    {
        // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseRouting();

    app.UseAuthentication();
    app.UseIdentityServer();
    app.UseAuthorization();

    app.MapControllerRoute(
        name: "default",
        pattern: "{controller}/{action=Index}/{id?}");

    app.MapRazorPages();

    app.MapFallbackToFile("index.html"); ;

    app.Run();

【问题讨论】:

  • 你能分享你的登录代码吗?
  • 提供您的登录逻辑。
  • 我没有写登录逻辑。该应用程序使用 asp.net 身份,内置登录功能。
  • 为了让您保持理智,我会将 IdentityServer 放在它的 on service 实例中,否则当您将 client/Identity 和 IdentityServer 混合在一起时出现问题时很难排除故障......跨度>

标签: asp.net-mvc asp.net-core asp.net-identity identityserver4 asp.net-authorization


【解决方案1】:

这里已经回答了这个问题:https://stackoverflow.com/a/62090053/3317709

事实证明,使用 IdentityServer 扩展方法会添加一个策略方案,以便只有 /Identity 页面具有 cookie 身份验证。其余默认为 JWT。

我们可以通过添加我们自己的策略来自定义它,如下所示:

builder.Services.AddAuthentication()
    .AddIdentityServerJwt()
    .AddPolicyScheme("ApplicationDefinedAuthentication", null, options =>
    {
        options.ForwardDefaultSelector = (context) =>
        {
            if (context.Request.Path.StartsWithSegments(new PathString("/Identity"), StringComparison.OrdinalIgnoreCase) ||
                context.Request.Path.StartsWithSegments(new PathString("/demo"), StringComparison.OrdinalIgnoreCase))
                return IdentityConstants.ApplicationScheme;
            else
                return IdentityServerJwtConstants.IdentityServerJwtBearerScheme;
        };
    });

// Use own policy scheme instead of default policy scheme that was set in method AddIdentityServerJwt 
builder.Services.Configure<AuthenticationOptions>(options => options.DefaultScheme = "ApplicationDefinedAuthentication");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-27
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    相关资源
    最近更新 更多